1.c# Managed and unmanaged code
C # has its own memory recycling mechanism, so in C # we can just new, do not care how delete,c# use GC to clean up memory, this part of memory is managed memories, most of the time we work in the C # environment, are using managed memory, but C # after all, run in C + +, sometimes, (like maybe we need to introduce a library of some third-party C + + or native code, which is common in unity3d development) we need to manipulate unmanaged code directly in C #, these non-managed Memory we need to deal with their application and release. In C #, there are interfaces to complete conversions between managed and unmanaged, as well as operations on this part of memory. There are basically the following types:
2.managed mem-> un-managed Mem
For example, a third-party C + + library is called in C #, and a function in the library is void func (float * data, int length). What we need to pass to data should be an unmanaged code (why?). First pass in the managed memory, the C # layer is likely to take it off, and C + + is still in use, and the managed mem its pointer address may change, so directly to C + + may get the address is wrong)
The code is as follows:
Using System.Runtime.InteropServices;
float[] _managed_data = ...//This is the C # managed data
GCHandle Unmanaged_data_handle = GCHandle.Alloc (_managed_data, gchandletype.pinned); This will mark the _managed_data temporarily cannot be reclaimed by GC, and the address of the fixed object
Func (Unmanaged_data_handle. Addrofpinnedobject (), _managed_data. LENGTH);//This will get the fixed address of the unmanaged memory and pass it to C + +
Unmanaged_data_handle. Free ();//After the use is complete, handle it to
3.un-managed mem->managed Mem, which returns a un-managed mem in C + + for use in C #
Sometimes it is necessary to allocate a piece of processed memory in C + + and then return it to C # for use, such as an interface int func (int** data) in C + + (note that pointers to pointers are used here because data is the result)
IntPtr Unmanaged_ptr=intptr.zero; Defines the type of pointer in C # that is used to receive C + + return data
int length = func (out unmanaged_ptr);//call C + + function, so that unmanaged_ptr points to the memory allocated in C + +, note that this is used out, in order to match the C + +.
byte[] Managed_data = new Byte[length];
Marshal.Copy (unmanaged_ptr, managed_data, 0, length);//copy unmanaged memory to managed memory for use in C #
Marshal.freehglobal (unmanaged_ptr);//Release unmanaged memory
4. Apply a un-managed mem to C + + directly in C #
Sometimes it is necessary to open a piece of unmanaged memory directly in C # and pass it to C + +, which can also be destroyed in C #. The code is as follows
IntPtr unmanaged_data_prt = Marshal. AllocHGlobal (+)//Direct allocation of memory in bytes
Func (UNMANAGED_DATA_PRT);//pass to C + + use
Marshal.freehglobal (UNMANAGED_DATA_PRT); destroy unmanaged memory after use
There are also many ways to handle unmanaged memory in the Marshal class. Managed memory and unmanaged within the C # inside can be free of each other's transformation, mainly through the Marshal class and GCHandle class, programming as long as the attention of the unmanaged memory must be responsible for the release of the good.
Conversion between C # managed memory and unmanaged memory (combined with the actual development of Unity3d)