0

First of all I'm not quite familiar with marshalling and dealing with unmanaged resources. I was migrating a project that was compiled and ran with mono and Framework v4.6.1. Basically what I had to do was only change the target framework, yet one thing that made everything stop working was trying to Pin an object into memory with GCHandle.Alloc. That threw an exception stating that Unhandled Exception: System.ArgumentException: Object contains non-primitive or non-blittable data.

What I find weird is that this allegedly (I take the word of the owner when he says that it runs successfully) this works when running with Mono. is there any difference on how .NET core and Mono treat memory ?

The exception is thrown when I do the following:

private ws2811_t _ws2811;
private GCHandle _ws2811Handle;

public WS281x(Settings settings)
{
    _ws2811 = new ws2811_t();
    //Pin the object in memory. Otherwise GC will probably move the object to another memory location.
    //This would cause errors because the native library has a pointer on the memory location of the object.
    _ws2811Handle = GCHandle.Alloc(_ws2811, GCHandleType.Pinned);

Here's the Object that I'm trying to pin:

[StructLayout(LayoutKind.Sequential)]
internal struct ws2811_t
{
    public long render_wait_time;
    public IntPtr device;
    public IntPtr rpi_hw;
    public uint freq;
    public int dmanum;
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = PInvoke.RPI_PWM_CHANNELS)]
    public ws2811_channel_t[] channel;
}


[StructLayout(LayoutKind.Sequential)]
internal struct ws2811_channel_t
{
    public int gpionum;
    public int invert;
    public int count;
    public int strip_type;
    public IntPtr leds;
    public byte brightness;
    public byte wshift;
    public byte rshift;
    public byte gshift;
    public byte bshift;
    public IntPtr gamma;
}

}

I've looked on both mono Blittable types and dotnet blittable Types but I'm not familiar with that Is this a possible bug, or simply misunderstanding on my part?

Daniel S.
  • 389
  • 1
  • 6
  • 14

1 Answers1

1

As stated here, the code was working on mono because mono had a bug. In my case, since my array only had 2 positions, I've created 2 fields and the problem was solved

Daniel S.
  • 389
  • 1
  • 6
  • 14