title: Mono compiler crashes when trying to compile a class that calls native code
body: <p>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 <strong>this</strong> class but it's still broken, so it seems like the issue is with this code specifically.</p>
  
  <p>Has anyone seen anything like this?</p>
  
  <pre><code>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 ();
  
      } 
  }
  </code></pre>
  
  <p>The compiler output is as follows:</p>
  
  <blockquote>
    <p>Building Solution: calling-native-code-mono (Debug)</p>
    
    <p>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" </p>
    
    <p>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<br>
    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<br>
    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 </p>
    
    <p>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<br>
    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<br>
    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</p>
    
    <p>---------------------- Done ----------------------</p>
    
    <p>Build: 1 error, 0 warnings</p>
  </blockquote>