"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?

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.

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.

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.