ExecutionEngineException : JIT/AOT error when adding to delegate

Using Unity 4.6.1

My code is crashing with an ExecutionEngineException when I am adding a handler to a delegate event. The delegate’s signature is

public delegate void OnDelegate (string channel, string message);

Its pretty simple with no generics. The delegate and the class in question are located inside a dll, if that has anything to do with it.

My exception is :

ExecutionEngineException: Attempting to JIT compile method ‘(wrapper managed-to-native) System.Threading.Interlocked:CompareExchange (MyDll.OnDelegate&,MyDll.OnDelegate,MyDll.OnDelegate)’ while running with --aot-only.

Please advise.

Hi,

You are probably compiling a dll with Visual Studio 2010 (or newer).
Its compiler generate code for your events (add and remove properties), involving a call to System.Threading.Interlocked.CompareExchange<T>(ref T, T, T) which is not aot friendly.

Consider either using Visual Studio 2008 or Mono toolchain to build your dll, or just write yourself the code for the event properties.

(Eg: )

EventHandler m_MyEvent
public event EventHandler MyEvent
{
  add { m_MyEvent += value; }
  remove { m_MyEvent -= value; }
}