Mono compiler crashes when trying to compile a class that calls native code

1.5k views Asked by At

I have the following class that maps some run loop calls from native code to C# using Mono (in a mac) and the compiler just doesn't work, it crashes all the time when trying to compile it. I have removed it to a separate project containing only this class but it's still broken, so it seems like the issue is with this code specifically.

Has anyone seen anything like this?

using System;
using System.Threading;

namespace Integration.Mac
{

    public class CommonRunLoop
    {

        static IntPtr runLoopReference;

        public static IntPtr CommonRunLoopReference {
            get {
                return runLoopReference;
            }
        }

        static CommonRunLoop() {

            AutoResetEvent lockObj = new AutoResetEvent (true);

            Thread runLoop = new Thread (delegate() {
                runLoopReference = CFRunLoopGetCurrent ();
                lockObj.Set ();
                CFRunLoopRun ();
            });

            runLoop.IsBackground = true;
            runLoop.Name = "Common-Sync-Run-Loop";
            lockObj.WaitOne();
        }

        [DllImport ("/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation")]
        extern static IntPtr CFRunLoopGetCurrent ();

        [DllImport ("/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation")]
        extern static IntPtr CFRunLoopGetMain ();

        [DllImport ("/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation")]
        extern static void CFRunLoopRun ();

    } 
}

The compiler output is as follows:

Building Solution: calling-native-code-mono (Debug)

Building: calling-native-code-mono (Debug) Performing main compilation... /Library/Frameworks/Mono.framework/Versions/2.10.9/bin/dmcs /noconfig "/out:/Users/mauricio/Projects/calling-native-code-mono/bin/Debug/calling-native-code-mono.dll" "/r:/Library/Frameworks/Mono.framework/Versions/2.10.9/lib/mono/4.0/System.dll" "/r:/Library/Frameworks/Mono.framework/Versions/2.10.9/lib/mono/4.0/System.Core.dll" /nologo /warn:4 /debug:full /optimize- /codepage:utf8 "/define:DEBUG" /t:library "/Users/mauricio/Projects/calling-native-code-mono/CommonRunLoop.cs" "/Users/mauricio/Projects/calling-native-code-mono/AssemblyInfo.cs"

Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object at Mono.CSharp.EmitContext.Emit (OpCode opcode, Mono.CSharp.MethodSpec method) [0x00000] in :0 at Mono.CSharp.New.Emit (Mono.CSharp.EmitContext ec, IMemoryLocation target) [0x00000] in :0 at Mono.CSharp.VariableReference.EmitAssign (Mono.CSharp.EmitContext ec, Mono.CSharp.Expression source, Boolean leave_copy, Boolean prepare_for_load) [0x00000] in :0 at Mono.CSharp.Assign.Emit (Mono.CSharp.EmitContext ec, Boolean is_statement) [0x00000] in :0 at Mono.CSharp.Assign.EmitStatement (Mono.CSharp.EmitContext ec) [0x00000] in :0 at Mono.CSharp.BlockVariableDeclaration.DoEmit (Mono.CSharp.EmitContext ec) [0x00000] in :0 at Mono.CSharp.Statement.Emit (Mono.CSharp.EmitContext ec) [0x00000] in :0 at Mono.CSharp.Block.DoEmit (Mono.CSharp.EmitContext ec) [0x00000] in :0 at Mono.CSharp.Block.Emit (Mono.CSharp.EmitContext ec) [0x00000] in :0 at Mono.CSharp.ExplicitBlock.Emit (Mono.CSharp.EmitContext ec) [0x00000] in :0 at Mono.CSharp.ToplevelBlock.Emit (Mono.CSharp.EmitContext ec) [0x00000] in :0 at Mono.CSharp.Constructor.Emit () [0x00000] in :0
at Mono.CSharp.TypeContainer.EmitType () [0x00000] in :0 at Mono.CSharp.ModuleContainer.Emit () [0x00000] in :0 at Mono.CSharp.AssemblyDefinition.Emit () [0x00000] in :0 at Mono.CSharp.Driver.Compile () [0x00000] in :0 at Mono.CSharp.Driver.Main (System.String[] args) [0x00000] in :0 [ERROR] FATAL UNHANDLED EXCEPTION: System.NullReferenceException: Object reference not set to an instance of an object at Mono.CSharp.EmitContext.Emit (OpCode opcode, Mono.CSharp.MethodSpec method) [0x00000] in :0 at Mono.CSharp.New.Emit (Mono.CSharp.EmitContext ec, IMemoryLocation target) [0x00000] in :0 at Mono.CSharp.VariableReference.EmitAssign (Mono.CSharp.EmitContext ec, Mono.CSharp.Expression source, Boolean leave_copy, Boolean prepare_for_load) [0x00000] in :0 at Mono.CSharp.Assign.Emit (Mono.CSharp.EmitContext ec, Boolean is_statement) [0x00000] in :0 at Mono.CSharp.Assign.EmitStatement (Mono.CSharp.EmitContext ec) [0x00000] in :0 at Mono.CSharp.BlockVariableDeclaration.DoEmit (Mono.CSharp.EmitContext ec) [0x00000] in :0 at Mono.CSharp.Statement.Emit (Mono.CSharp.EmitContext ec) [0x00000] in :0 at Mono.CSharp.Block.DoEmit (Mono.CSharp.EmitContext ec) [0x00000] in :0 at Mono.CSharp.Block.Emit (Mono.CSharp.EmitContext ec) [0x00000] in :0 at Mono.CSharp.ExplicitBlock.Emit (Mono.CSharp.EmitContext ec) [0x00000] in :0 at Mono.CSharp.ToplevelBlock.Emit (Mono.CSharp.EmitContext ec) [0x00000] in :0 at Mono.CSharp.Constructor.Emit () [0x00000] in :0
at Mono.CSharp.TypeContainer.EmitType () [0x00000] in :0 at Mono.CSharp.ModuleContainer.Emit () [0x00000] in :0 at Mono.CSharp.AssemblyDefinition.Emit () [0x00000] in :0 at Mono.CSharp.Driver.Compile () [0x00000] in :0 at Mono.CSharp.Driver.Main (System.String[] args) [0x00000] in :0

Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object at Mono.CSharp.EmitContext.Emit (OpCode opcode, Mono.CSharp.MethodSpec method) [0x00000] in :0 at Mono.CSharp.New.Emit (Mono.CSharp.EmitContext ec, IMemoryLocation target) [0x00000] in :0 at Mono.CSharp.VariableReference.EmitAssign (Mono.CSharp.EmitContext ec, Mono.CSharp.Expression source, Boolean leave_copy, Boolean prepare_for_load) [0x00000] in :0 at Mono.CSharp.Assign.Emit (Mono.CSharp.EmitContext ec, Boolean is_statement) [0x00000] in :0 at Mono.CSharp.Assign.EmitStatement (Mono.CSharp.EmitContext ec) [0x00000] in :0 at Mono.CSharp.BlockVariableDeclaration.DoEmit (Mono.CSharp.EmitContext ec) [0x00000] in :0 at Mono.CSharp.Statement.Emit (Mono.CSharp.EmitContext ec) [0x00000] in :0 at Mono.CSharp.Block.DoEmit (Mono.CSharp.EmitContext ec) [0x00000] in :0 at Mono.CSharp.Block.Emit (Mono.CSharp.EmitContext ec) [0x00000] in :0 at Mono.CSharp.ExplicitBlock.Emit (Mono.CSharp.EmitContext ec) [0x00000] in :0 at Mono.CSharp.ToplevelBlock.Emit (Mono.CSharp.EmitContext ec) [0x00000] in :0 at Mono.CSharp.Constructor.Emit () [0x00000] in :0
at Mono.CSharp.TypeContainer.EmitType () [0x00000] in :0 at Mono.CSharp.ModuleContainer.Emit () [0x00000] in :0 at Mono.CSharp.AssemblyDefinition.Emit () [0x00000] in :0 at Mono.CSharp.Driver.Compile () [0x00000] in :0 at Mono.CSharp.Driver.Main (System.String[] args) [0x00000] in :0 [ERROR] FATAL UNHANDLED EXCEPTION: System.NullReferenceException: Object reference not set to an instance of an object at Mono.CSharp.EmitContext.Emit (OpCode opcode, Mono.CSharp.MethodSpec method) [0x00000] in :0 at Mono.CSharp.New.Emit (Mono.CSharp.EmitContext ec, IMemoryLocation target) [0x00000] in :0 at Mono.CSharp.VariableReference.EmitAssign (Mono.CSharp.EmitContext ec, Mono.CSharp.Expression source, Boolean leave_copy, Boolean prepare_for_load) [0x00000] in :0 at Mono.CSharp.Assign.Emit (Mono.CSharp.EmitContext ec, Boolean is_statement) [0x00000] in :0 at Mono.CSharp.Assign.EmitStatement (Mono.CSharp.EmitContext ec) [0x00000] in :0 at Mono.CSharp.BlockVariableDeclaration.DoEmit (Mono.CSharp.EmitContext ec) [0x00000] in :0 at Mono.CSharp.Statement.Emit (Mono.CSharp.EmitContext ec) [0x00000] in :0 at Mono.CSharp.Block.DoEmit (Mono.CSharp.EmitContext ec) [0x00000] in :0 at Mono.CSharp.Block.Emit (Mono.CSharp.EmitContext ec) [0x00000] in :0 at Mono.CSharp.ExplicitBlock.Emit (Mono.CSharp.EmitContext ec) [0x00000] in :0 at Mono.CSharp.ToplevelBlock.Emit (Mono.CSharp.EmitContext ec) [0x00000] in :0 at Mono.CSharp.Constructor.Emit () [0x00000] in :0
at Mono.CSharp.TypeContainer.EmitType () [0x00000] in :0 at Mono.CSharp.ModuleContainer.Emit () [0x00000] in :0 at Mono.CSharp.AssemblyDefinition.Emit () [0x00000] in :0 at Mono.CSharp.Driver.Compile () [0x00000] in :0 at Mono.CSharp.Driver.Main (System.String[] args) [0x00000] in :0 Build complete -- 1 error, 0 warnings

---------------------- Done ----------------------

Build: 1 error, 0 warnings

1

There are 1 answers

0
Maurício Linhares On BEST ANSWER

Adding:

using System.Runtime.InteropServices;

At the beginning of the file fixed it, DLLImport is at that namespace. Not sure why the compiler didn't just complain about it instead of crashing.

A bug report was filled here.