Unmanaged DLL causing AccessViolationException

左心房为你撑大大i 提交于 2019-12-06 08:43:03

Your problem can be inferred from the name of the function. When you "set a buffer", it is likely that the native code is going to use that buffer later. That's incompatible with the garbage collector, it is going to move the array when it compacts the heap. That's a big Kaboom when the native code then writes to the buffer, it is going to write to memory that isn't there anymore. The most typical outcome is a FatalExecutionEngineException when the garbage collector detects that the heap integrity got compromised.

The array needs to be pinned, something that the pinvoke marshaller does when it calls the function, but it unpins the array after the call.

You can pin a managed array with GCHandle.Alloc(), but that's pretty detrimental to the garbage collector if you leave it pinned for a long time. By far the best solution is to use Marshal.AllocHGlobal to allocate a chunk of unmanaged memory, it never moves.

If you still have trouble then worry about the size of the buffer. And just plain misery in the native code, it rarely needs much help to fall over on an AccessViolation. That's the standard failure mode for native code. Very hard to diagnose, impossible if you don't have the source code for it. Contact the code owner for support, have a small repro snippet available to help him locate the problem.

I agree that it's likely that datasource is the problem.

Try changing the dllimport so that it's using an IntPtr rather than the byte[].

[DllImport("MyDll.dll", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
static private extern unsafe void SetDataBuffer(Int32 windowHandle, IntPtr dataSource, Int32 format, Int32 stride, Int32 totalBufferSize);

Then when you're calling it, explicitly allocate the memory for the buffer like so:

IntPtr buffer = Marshal.AllocHGlobal(windowWidth * windowHeight * bytesPerPixel);
SetDataBuffer(windowHandle, buffer, (Int32)0, (Int32)(windowWidth * bytesPerPixel), (Int32)(windowWidth * windowHeight * bytesPerPixel));

And don't forget to call Marshal.FreeHGlobal(buffer) when you're done done with it.

I take it that the buffer is being used internally by the Update method?

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!