C# has its own memory recycling mechanism, so in C# we can just new without caring about how to delete it. C# uses gc to clean up memory. This part of the memory is managed memory. Most of the time we work in the C# environment and use managed memory. However, C# runs on C++ after all, and sometimes, (for example, we may need to introduce some third-party C++ or native code libraries, which is very common in Unity3d development) we need to directly manipulate unmanaged code in C#. We need to process their application and release these non-managed memory by ourselves. C# provides some interfaces to complete the conversion between managed and unmanaged, as well as operations on this part of the memory.
There are basically the following:
memory-> unmanaged memory
For example, in c#, a function in the library is void func(float * data, int length). What we need to pass to data should be a non-managed code (why? First pass into the managed memory, the c# layer will likely gc it, and c++ is still in use, and the pointer address of the managed mem may change, so it is wrong to pass it directly to the address that may be obtained by c++)
The code is as follows:
using ; float[] _managed_data =... // this is the c# managed data GCHandle unmanaged_data_handle = (_managed_data, ); //The marker _managed_data cannot be recycled by gc for the time being, and the address of the object is fixedfunc(unmanaged_data_handle.AddrOfPinnedObject(),_managed_data.Length);// Here you will get the fixed address of unmanaged memory and pass it to c++unmanaged_data_handle.Free();//After use,Put ithandle free,soc#It can be normal to gc memory
-managed memory->managed memory
Return an un-managed mem in c++ for use in c#. Sometimes you need to allocate a piece of processed memory in C++ and then return it to C# for use, such as an interface in C++ int func(int** data) (note that pointer is used here, because data is the result)
IntPtr unmanaged_ptr=; //Define this cThe pointer type used in # to receive data returned by c++int length = func(out unmanaged_ptr ); //Call the c++ function to make unmanaged_ptr point to the memory allocated in c++. Note that out is used here to match the ** in c++.byte[] managed_data = new byte[length]; (unmanaged_ptr, managed_data, 0, length);//Copy unmanaged memory into managed memory to release unmanaged memory in c#Use inside(unmanaged_ptr);//Release unmanaged memory in c#Use inside(unmanaged_ptr);//
3. Apply for an un-managed mem to pass to c++ in c#
Sometimes you need to directly open a piece of unmanaged memory in C# and pass it to C++ for use. This piece of memory can also be destroyed after use in C#. The code is as follows
IntPtr unmanaged_data_prt = Marshal. AllocHGlobal(100);// Directly allocate 100 bytes of memoryfunc(unmanaged_data_prt);//Passed to c++ for use(unmanaged_data_prt);Destroy unmanaged memory after use
In addition, there are many methods for handling unmanaged memory in the Marshal class.
Remark
Hosted memory and unmanaged memory can be converted freely in C#, mainly through the Marshal class and GCHandle class. When programming, just pay attention to the unmanaged memory and must be responsible for releasing it. Thank you for your support.