- Home /
"ExecutionEngineException: Attempting to JIT compile method" when Marshal.PtrToStructure function operating on iphone
There was an error "ExecutionEngineException: Attempting to JIT compile method" when I operate Marshal.PtrToStructure function on iphone, how can I sovel it?
Answer by rutter · May 10, 2012 at 06:37 PM
When running on iOS, JIT ("just in time") compilation is limited or impossible, and all of your code must be compiled AOT ("ahead of time"). Depending on the particular error, there are two common causes:
Your application makes use of some generic type that was missed during AOT compile.
Your application uses reflection or other dynamically generated code.
The first problem can usually be fixed by including a "dummy" class that references the missing types.
The second problem is a doozy, and I think is best avoided if possible. I'd guess that you're running into this one.
tl;dr If you're not sure what any of this means, you've probably imported a library that's not compatible with iOS.
Good question and good answer. For me, the problem was having a generic method that forwarded its T
to another generic method. The solution was to call that second method, with all the possible types of T
in some dummy method inside the class.
But in the case of using dynamically generated codes, just like those genereted for a wsdl SOAP service?
Just ran into this problem. Turned out this code does not work on iOS, but works well in Unity: http://www.codeproject.com/$$anonymous$$B/tips/SerializedObjectCloner.aspx I used it to deep clone an object. Any solid way of serializing/cloning C# objects in Unity?
Answer by mviuk · Jun 18, 2012 at 09:13 AM
I had this issue when attempting to use System.Net.WebClient, it was working for several weeks, then stopped working and I got the same error. Turns out I'd changed the API Compatibility Level to .NET 2.0 rather than .NET 2.0 Subset, changing it back to the subset resolved the issue. Kind of weird that the Subset works but .NET 2.0 (which I'd assume to be the superset) doesn't.
Yeah it has to do with the fact that Subset uses a different non-generic method of getting some properties and fields - so Subset is slower, but happens to work on IOS in this case because it doesn't need the generic types that get JIT compiled. There are work arounds you can use to make the full version work like this which work in some circumstances.
Holy crap this fixed a major issue I was having. Thanks! +1 if I could.
Answer by demonpants · Jan 27, 2013 at 01:07 AM
I was not using any reflection or any other sneaky stuff, and I also was not using dynamic types anywhere. However, I was getting this issue as well. The problem came down to dynamic arrays, like this:
int[,,] array3d;
It actually wouldn't freak out until I instantiated the array, then would refuse to JIT the function that was doing the instantiation. Like this:
public void ThisWillBreakInAOT()
{
array3d = new int[ 10, 15, 20 ];
}
So for those of you who aren't masters with C#'s inner workings and were thinking that you should always do multidimensional arrays with that syntax, now you know – don't do it if you want to run on iPhone. I ended up just flattening all my arrays into 1D arrays since that's faster to use anyway.
Can you please share what is the proper way to initialize the array?
As I said, I flattened by arrays, and made a class to simplify access. So, like this:
int[] array3d = new int[ width * height * length ]; array3d[ i * width * height + j * height + k ] = value;
Also, I think this sort of array works:
int[][][] array3d = new int[width][height][length];
But I recall that behaving weirdly in C# so I avoided using it.