IOS Marshal.PtrToStructure System.ExecutionEngineException

597 Views Asked by At

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?

0

There are 0 best solutions below