Bug 3682 - System.ExecutionEngineException: Attempting to JIT compile method '(wrapper delegate-invoke)
Summary: System.ExecutionEngineException: Attempting to JIT compile method '(wrapper d...
Alias: None
Product: iOS
Classification: Xamarin
Component: Xamarin.iOS.dll ()
Version: 5.2
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Sebastien Pouliot
Depends on:
Reported: 2012-02-29 07:57 UTC by Michal
Modified: 2012-03-01 10:18 UTC (History)
4 users (show)

Is this bug a regression?: ---
Last known good build:

Samples demonstrating the problem. (7.68 KB, application/zip)
2012-02-29 07:57 UTC, Michal

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Michal 2012-02-29 07:57:58 UTC
Created attachment 1440 [details]
Samples demonstrating the problem.

Calling AddEventHandler in this piece of code throws exception on device (works on simulator):

var e = _object.GetType().GetEvent("Event");
if (e != null)
 e.AddEventHandler(_object, new EventHandler(OnEvent));

Check attached sample for full code.

Unhandled Exception: System.ExecutionEngineException: Attempting to JIT compile method '(wrapper delegate-invoke) <Module>:invoke_void__this___AppDelegate/SomeObject_EventHandler (AddHandler.AppDelegate/SomeObject,System.EventHandler)' while running with --aot-only.

  at System.Reflection.EventInfo.AddEventFrame[SomeObject,EventHandler] (System.Reflection.AddEvent`2 addEvent, System.Object obj, System.Object dele) [0x00000] in <filename unknown>:0 
  at System.Reflection.EventInfo.AddEventHandler (System.Object target, System.Delegate handler) [0x0006e] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Reflection/EventInfo.cs:100 
  at AddHandler.AppDelegate.FinishedLaunching (MonoTouch.UIKit.UIApplication app, MonoTouch.Foundation.NSDictionary options) [0x00063] in /Users/michalsartoris/Projects/Bugs.MonoTouch/AddHandler/AddHandler/AppDelegate.cs:53 
  at MonoTouch.UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x00042] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:29 
  at AddHandler.Application.Main (System.String[] args) [0x00000] in /Users/michalsartoris/Projects/Bugs.MonoTouch/AddHandler/AddHandler/Main.cs:17
Comment 1 Sebastien Pouliot 2012-02-29 15:26:42 UTC
Workaround from the bug reporter (from the original email thread):

> It looks like this is not related to linker. I set it to "Don't link", but the result was the same.
> This code works:
> var e = dataSource.GetType().GetEvent("MoveNextCompleted");
> if (e != null)
> {
>        //e.AddEventHandler(dataSource, new EventHandler(AsyncEnumerable_MoveNextCompleted));
>        var add = e.GetAddMethod();
>        add.Invoke(dataSource, new object[] { new EventHandler(AsyncEnumerable_MoveNextCompleted) });
> }
Comment 2 Sebastien Pouliot 2012-03-01 10:18:45 UTC
The optimization in Mono's mscorlib EventInfo type uses generics where the types are not known before runtime (and that's a problem when AOT-ing). Your workaround basically to what the unoptimized code path is doing. 

I'm disabling this optimization in MonoTouch 5.3.1+. In the meantime your workaround is ok (and will continue to be ok).

Thanks for the test case (it's now part of our test suite :-).

master: 0dda6832d4f8bf7907ef379241ca2ebe807196a5
QA: unit test included in 'linksdk' solution