0

Hey I was wondering if there is a fix for the following issue as i can't seem to find one anywhere and I have now ran out of ideas.

I am writing a program using Xamarin.Forms to run across both android and IOS, the following both code versions work on android, However throws a ExecutionEngineException when the second version is ran on IOS but the first one works.

protected static object ReadStruct(BinaryReader reader, Type structType, ChunkHeader chunkHeader)
{
    int size = Marshal.SizeOf(structType);
    if (size != chunkHeader.chunkSize) // check the actual size of this chunk object with the expected size from the stream
    {
        throw new IOException("Invalid file format, incorrect " + chunkHeader.chunkName + " chunk size (exp.: " + size + ", read: " + chunkHeader.chunkSize + ")");
    }

    byte[] data = reader.ReadBytes(size);
    IntPtr buffer = Marshal.AllocHGlobal(size);
    Marshal.Copy(data, 0, buffer, size);
 // the line that crashes follows this
    object structObject = Marshal.PtrToStructure(buffer, structType);
    Marshal.FreeHGlobal(buffer);
    return structObject;
}

The above code stays the same in both versions.

public struct OvdBChunk 
{
   // stuff in here but not important
}

The above sample works but for this and all future updates i will need to inherit the old versions as stuff may have been updated in newer devices so have been added to but will always keep the old stuff so i changed it to the following

public class OvdBChunk : OvdAChunk
{
   // stuff in here but not important
}

The structType in part is the part that is changing in the top code snippet.

Any idea why when it is a class instead of a strut it throws the System.ExecutionEngineException. and any ideas on how i can fix it?

  • Are you specifying the layout using the StructLayoutAttribute? – Andrew Tavera Aug 15 '16 at 14:02
  • yes it is `[StructLayout(LayoutKind.Sequential, Pack = 1)]` – user6430870 Aug 15 '16 at 14:22
  • I tried creating a minimal example of this and it ran without errors on the simulator, but on a device, I got the same exception with a link to the Xamarin iOS limitations page. I don't see any mention of this on the limitations page so it may be worth filing a bug report to Xamarin. – Andrew Tavera Aug 15 '16 at 15:56
  • Thank you added an eeor report here: https://bugzilla.xamarin.com/show_bug.cgi?id=43404 if anyone is intrested in it – user6430870 Aug 16 '16 at 07:57

0 Answers0