简体   繁体   English

在C ++ dll中分配内存,在C#中免费

[英]Allocate memory in a C++ dll, free in C#

I am new in C# and trying to communicate C# with C++ dll. 我是C#的新手,正在尝试与C ++ dll进行C#通信。 My main purpose is allocating memory in C# using Marshal.AllocHGlobal() , assign values in it and pass the reference of IntPtr to C++ dll. 我的主要目的是使用Marshal.AllocHGlobal()在C#中分配内存,在其中分配值,并将IntPtr的引用传递给C ++ dll。 On the side of C++ dll, I'm changing the allocated memory size with malloc() , assign variables. 在C ++ dll方面,我正在使用malloc()更改分配的内存大小,分配变量。 In the end, I print values in C#. 最后,我在C#中打印值。 Until this point, everything works fine. 到目前为止,一切正常。

But when I try to free allocated memory in C# with Marshal.FreeHGlobal() , I get an error. 但是,当我尝试使用Marshal.FreeHGlobal()在C#中释放分配的内存时,出现错误。

Actually, I search a lot to how I should implement my code with these functions. 实际上,我在如何使用这些功能实现代码方面进行了大量研究。 I found a solution that says I must write a function in the DLL for deallocating the memory and call this function. 我找到了一个解决方案,说我必须在DLL中编写一个用于释放内存的函数并调用此函数。 But my manager told me, I need to do this work in C# side. 但是我的经理告诉我,我需要在C#端完成这项工作。 So I can't use that solution. 因此,我无法使用该解决方案。 Also, I found some other ideas which use COM but the manager doesn't want to use COM. 另外,我发现了其他一些使用COM的想法,但管理器不想使用COM。 So I can't use COM. 所以我不能使用COM。 Here some codes; 这里有一些代码;

Allocating the memory in C# 在C#中分配内存

var offset = 0;
var size = Marshal.SizeOf(typeof(dVehicle)) * denemeVehicle.Count();
IntPtr vehPnt;
vehPnt = Marshal.AllocHGlobal(size);

for(var i = 0; i < denemeVehicle.Count(); i++)
{
    Marshal.StructureToPtr(denemeVehicle[i], new IntPtr(vehPnt.ToInt64() + offset), false);
    offset += Marshal.SizeOf(typeof(dVehicle));
}

Changing the allocated memory size in C++ 在C ++中更改分配的内存大小

*vehicles = (dVehicle*) malloc(sizeof(dVehicle) * (new_VehSize));

Free the allocated memory 释放分配的内存

Marshal.FreeHGlobal(vehPnt); // The error shows up in here

And this is error message; 这是错误消息;

The handle is invalid. 句柄无效。 (Exception from HRESULT: 0x80070006 (E_HANDLE)) (来自HRESULT的异常:0x80070006(E_HANDLE))

Marshal.FreeHGlobal() cannot free memory that was allocated with the C malloc() function. Marshal.FreeHGlobal()无法释放使用C malloc()函数分配的malloc() The memory must be "from the global heap allocated by [Marshal.]AllocHGlobal, [Marshal.]ReAllocHGlobal, or any equivalent unmanaged API method ." 内存必须“来自[Marshal。] AllocHGlobal,[Marshal。] ReAllocHGlobal 或任何等效的非托管API方法分配的全局堆”。

Internally, Marshal.AllocHGlobal() and Marshal.FreeHGlobal() use the Win32 LocalAlloc() and LocalFree() functions. 在内部, Marshal.AllocHGlobal()Marshal.FreeHGlobal()使用Win32 LocalAlloc()LocalFree()函数。 This is documented on MSDN. 这记录在MSDN上。

Otherwise, if you want the DLL to allocate the memory however it wants, you will have to make the C# code pass the allocated pointer back to the DLL so it can be freed correctly, ie using the C free() function if the memory is allocated with malloc() . 否则,如果您希望DLL根据需要分配内存,则必须使C#代码将分配的指针传递回DLL,以便可以正确释放它,即,如果内存为DLL,则使用C free()函数。用malloc()分配。

If two modules share memory with each other, but do not agree on a common memory manager (such as one provided by the OS), then the module that allocates the memory must be the same module to free it, as it is the only module that knows how the memory was allocated and thus how to free it. 如果两个模块彼此共享内存,但在公共内存管理器(例如操作系统提供的内存管理器)上不一致,则分配内存的模块必须是同一模块才能释放它,因为它是唯一的模块知道如何分配内存以及如何释放内存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM