I know using the /unsafe
flag in C#, you can use pointers. In C/C++ to delete a pointer you would use free(pointer);
and delete pointer;
respectively. However, how would you achieve the same effect with the C# pointers?
It depends. You use free
and delete
to free memory allocated with malloc
and new
.
but
in general if you do a PInvoke call, then the pointer should be a IntPtr
.
if you use fixed
(or GCHandle
) to obtain a pointer for a managed object, then the memory was allocated from the GC memory
- For the memory of GC, when you un-pin that memory (exit the
fixed
block, or release theGCHandle
), the GC will return handling it - For memory allocated through .NET
Marshal
methods you use the complementaryFree
method - For memory received from native methods, you have to use the "correct" native method to free it.
Example of pinning memory received by the .NET:
int[] arr = new int[5];
fixed (int* p = arr)
{
// here arr is fixed in place and it won't be freed/moved by gc
}
// here arr is un-fixed and the GC will manage it
or, nearly equivalent (but a little less safe, because the unpinning is done manually)
GCHandle handle = GCHandle.Alloc(arr, GCHandleType.Pinned);
int* p2 = (int*)handle.AddrOfPinnedObject();
// here arr is fixed in place and it won't be freed/moved by gc
handle.Free();
// here arr is un-fixed and the GC will manage it
Example of allocating some memory from the "native" pool (through the allocator normally used by COM objects) by using Marshal.AllocCoTaskMem
(note that Marshal.AllocCoTaskMem
calls the CoTaskMemAlloc
of Windows API, so you can use both Marshal.FreeCoTaskMem
and the Windows API CoTaskMemFree
to free it):
// allocating space for 1000 chars
char* p3 = (char*)Marshal.AllocCoTaskMem(1000 * sizeof(char));
// here you can use p3
// and here you free it
Marshal.FreeCoTaskMem((IntPtr)p3);
or with another allocator supported by Marshal
(this is the one normally used by Windows API):
// allocating space for 1000 chars
char* p4 = (char*)Marshal.AllocHGlobal(1000 * sizeof(char));
// here you can use p4
// and here you free it
Marshal.FreeHGlobal((IntPtr)p4);
Let's say you have some Native code that gives you access to some memory where it saves some data:
static extern IntPtr GetSomeMemoryFromSomeWinApi();
static extern void FreeSomeMemoryFromSomeWinApi(IntPtr ptr);
You use like this:
IntPtr p5 = GetSomeMemoryFromSomeWinApi();
// here you have some memory received from some native API
// and here you free it
FreeSomeMemoryFromSomeWinApi(p5);
In this case it's your library that has to give you a Free
method, because you don't know how the memory was allocated, but sometimes your library's documentation tells you that the memory is allocated through a specific allocator, so you use that type of deallocator to free it, like
Marshal.FreeCoTaskMem(p5);
if the API was some COM object.
The Marshal
class even has the allocator for BSTR
(Unicode strings used by COM objects. They have their length pre-pendend)
string str = "Hello";
char *bstr = (char*)Marshal.StringToBSTR(str);
Marshal.FreeBSTR((IntPtr)bstr);
They have special handling because their "real" start address is like (bstr - 2) (they had an Int32
prepended with their length)
The point is that there are as many allocators as the grain of sand of the desert and the stars of the sky. Every one of them (with the exception of the standard one of .NET, the one used by new
) has a corresponding deallocator. They go like husband and wife. They don't mix with others.
As a final note, if you write mixed .NET/native C or C++ code, you'll have to expose some C/C++ methods that call their free
/delete
, because their free
/delete
are part of their C/C++ libraries, not of the OS.
There is new functionality in .NET 6 to allocate native memory using C APIs and that is using the new NativeMemory. Using this new method you can (have to) delete the allocated memory easily:
using System.Runtime.InteropServices;
unsafe
{
byte* buffer = (byte*)NativeMemory.Alloc(100);
NativeMemory.Free(buffer);
}
© 2022 - 2024 — McMap. All rights reserved.