Unhanded exception marshaling a struct only when compiling .NET Native tool chain

1

I'm working in an app created with Xamarin. It uses marshaling to copy a block of bytes into some structures. Android version and UWP version -without native compilation- works fine. But if I activate the compilation with native tool chain I get an unhandled exception. This is the code:

void WorkProcedure()
{
    var data1 = new MainStruct1();
    var data2 = new MainStruct2();

    IntPtr ptr1 = Marshal.AllocHGlobal(Marshal.SizeOf(data1));
    IntPtr ptr2 = Marshal.AllocHGlobal(Marshal.SizeOf(data2));

    // this call works fine
    Marshal.StructureToPtr(data2, ptr2, false);

    // this call explodes
    Marshal.StructureToPtr(data1, ptr1, false);
    }


    [StructLayout(LayoutKind.Sequential, Pack = 1)]
    private struct MainStruct1
    {
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 10)]
        public InnerStruct1[] arrayExample;
    }


    [StructLayout(LayoutKind.Sequential, Pack = 1)]
    private struct MainStruct2
    {
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 10)]
        public InnerStruct2[] arrayExample;
    }


    // using this struct, exception is fired
    [StructLayout(LayoutKind.Sequential, Pack = 1)]
    private struct InnerStruct1
    {
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 16)]
        public string strExample;
    }


    // using this struct, all code works
    [StructLayout(LayoutKind.Sequential, Pack = 1)]
    private struct InnerStruct2
    {
        public uint uintExample;
    }

The exception is:

Unhandled exception at 0x00007FFCF4CFC09C (mrt100_app.dll) in xxx.xxx.xxx: 0xC0000602

and it raises in this line where you get a pointer of the structure with an array of other structure (with other array inside) inside:

// this call explodes
Marshal.StructureToPtr(data1, ptr1, false);

When the execution stops, Visual Studio opens an assembly window of this file/class/method:

McgInterop.ComCallHelpers.ComCall__HRESULT(System.__ComObject, System.RuntimeTypeHandle, int, void*)

The problem is only ocours when you have a struct where any of it's elements is an array of other struct and this inner struct has any array (or string).

I'm using VS2015 Community Version 14.0.25425.01 Update 3, .NET Framework Version 4.7.02046

It looks like any kind of bug, but I would like to consult here before report it.

Any help will be appreciated.

c#
.net
visual-studio
xamarin
data-structures
asked on Stack Overflow Jul 11, 2017 by FDguez

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0