首先,我不太熟悉编组和处理非托管资源。
我正在迁移一个使用mono和Framework v4.6.1编译和运行的项目。基本上我必须做的只是改变目标框架,但是让一切停止工作的一件事是尝试用GCHandle.Alloc
将对象固定到内存中。这引起了一个例外,说明了这一点
Unhandled Exception: System.ArgumentException: Object contains non-primitive or non-blittable data.
我觉得奇怪的是,据说这个(我在he says that it runs successfully时接受了所有者的话)这与Mono一起运行时有效。 .NET core和Mono如何处理内存有什么不同?
执行以下操作时抛出异常:
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);
这是我试图确定的对象:
[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;
}
}
我已查看单Blittable types和dotnet blittable Types,但我不熟悉 这是一个可能的错误,还是仅仅是我的误解?