分配“非托管” C# 中的内存
我正在用 C# 编写一个使用 C++ 库的程序,由于某种原因,我需要分配一个非托管缓冲区以将其传递给库。有没有办法在 c# 中做到这一点? 基本上我只需要在 C# 中进行 malloc...
谢谢
I'm writting a program in c# that uses a C++ library, and for some reason I need to allocate an unmanaged buffer to pass it to the lib. Is there a way to do this in c# ?
Basically I would just need to do a malloc in C#...
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试这样的操作:
这使用
Marshal.AllocHGlobal
< /a> 方法:Try something like this:
This uses the
Marshal.AllocHGlobal
method:您还可以为此使用字节数组。
您可以通过使用不安全的例程和固定语句来完成此操作:
问题(这是一个重要的问题)是,在返回并且代码退出固定块后,不允许 SomeUnmanagedFunction 接触该指针。因此,如果你做这样的事情:
你只会带来麻烦。在这种情况下,您可能想要使用 GCHandle,它可以将托管对象固定在堆中。这也可能很麻烦,因为您需要及时取消固定它,否则您可能会面临堆碎片的风险。
一般来说,我建议确保您正确地 P/Invoking 到函数中,以便编组器可以为您完成这项工作。与 GlobalAlloc 相比,我更喜欢fixed,因为它的范围很明确。我无法决定 GlobalAlloc 和 GCHandle 中我最不喜欢哪一个。两者都要求您做更多的工作,因为 GC 或语言不会为您做这些工作。
You can also use a byte array for this.
You do this by using an unsafe routine and the fixed statement:
The issue - and this is an important one - is that SomeUnmanagedFunction is not allowed to touch that pointer after it has returned and code has exited the fixed block. So if you do something like this:
you are asking for nothing but trouble. In this case you probably want to use a GCHandle, which can pin a managed object in the heap. This can also be troublesome in that you NEED to unpin it in a timely manner or you risk fragmenting your heap.
In general, I would recommend making sure that you're P/Invoking correctly into the function so that the maybe marshaller can do this work for you. I like fixed better than GlobalAlloc since its scope is clear. I can't decide which I like least of GlobalAlloc and GCHandle. Both require you to do more work since the GC or language won't do it for you.
这就是我们需要使用特定字节数来分配和释放非托管内存的方式。
This is how we need to assign and free unmanaged memory by using specific number of bytes.
几年后,与此同时:
在 .net6、7 中,MS 为我们提供了“NativeMemory”类,其中包含“AlignedAlloc”等方法,这对于矢量化应用程序可能很有用:MS 在本示例中写道:
“此方法是 Clined_alloc API 或依赖于平台的对齐分配 API(例如 Win32 上的 _aligned_malloc)的精简包装。”
当然,在这种情况下就没有垃圾收集。必须使用“AlignedFree”自行释放内存以避免内存泄漏。
Years later, in the meantime:
In .net6, 7, MS gifted us with the class "NativeMemory" with methods like "AlignedAlloc", which might be useful for vectorized applications: MS writes for this example:
"This method is a thin wrapper over the C aligned_alloc API or a platform dependent aligned allocation API such as _aligned_malloc on Win32."
Of course, no garbage collection in that case. One has to release the memory oneself to avoid memory leaks, by using "AlignedFree".