Why does Marshal.PtrToStructure with nested arrays throw an ExecutionEngineException?

603 Views Asked by At

I am trying to convert a byte[] to a struct that contains an array of a struct that contains an array.

Something like this:

[StructLayout(LayoutKind.Sequential, Pack = 4)]
public struct Util_VersionType 
{
      [MarshalAs(UnmanagedType.ByValArray, ArraySubType = UnmanagedType.LPStr, SizeConst = 5000)]
      public DataItemsString[] Data; 
}

[StructLayout(LayoutKind.Sequential, Pack = 4)]
public struct DataItemsString {
     [MarshalAs(UnmanagedType.ByValArray, ArraySubType = UnmanagedType.U1, SizeConst = 10)]
     public System.Byte[] Value;
     public System.UInt32 Validity;
}

I use Marshal.PtrToStructure

Sometimes there is no problem but in about 100 attempts the CLR throws an uncatchable ExecutionEngineException.

Please help. I'm lost.

1

There are 1 best solutions below

0
Guy P On

Looks like that this Exception is not related to the marshaling of the above structure directly.

Please take a look in your code for cross accessing to the same instance by another thread.