SoFunction
Updated on 2025-03-08

C# Release Issues and Interpretations on Unmanaged Memory

Regarding the release of unmanaged memory

hardware:Dahua SDK

Software Platform:win10+vs2015

background:Recently, when collecting Dahua Industrial Camera SDK, I used to convert the managed code into unmanaged pointer memory. Because the memory pointer was not released in time, the memory of the pc kept rising. After checking the code, I found that it was because of the memory pointer, so I used it. (pData); Release the managed memory pointer, and the memory does not increase when the loop is running. Through this test, it was found that during the loop acquisition process, the RGB format is converted to Hobject. Hobject-type iamge images can be dispose without dispose, and will not cause memory overflow. This is a bit different from my previous understanding. In the loop acquisition process, I would dispose during the loop acquisition process and prevent the release from being unclear. However, after testing, I found that it is okay not to release, so I did not release it. This makes it convenient for me to extract the data in the memory without worrying about being dispose.

Conversion between managed and unmanaged memory

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's OKgcThis 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,Only inc#Use inside(unmanaged_ptr);//Free unmanaged memory

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.

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.