System.Runtime.InteropServices.Marshal.FreeHGlobal Method

Frees memory previously allocated from the unmanaged memory of the process.

Syntax

[System.Runtime.ConstrainedExecution.ReliabilityContract(System.Runtime.ConstrainedExecution.Consistency.WillNotCorruptState, System.Runtime.ConstrainedExecution.Cer.Success)]
public static void FreeHGlobal (IntPtr hglobal)

Parameters

hglobal
The handle returned by the original matching call to Marshal.AllocHGlobal(IntPtr).

Remarks

You can use Marshal.FreeHGlobal(IntPtr) to free any memory from the global heap allocated by Marshal.AllocHGlobal(IntPtr), Marshal.ReAllocHGlobal(IntPtr, IntPtr), or any equivalent unmanaged API method. If the hglobal parameter is IntPtr.Zero the method does nothing.

Marshal.FreeHGlobal(IntPtr) exposes the tp://go.microsoft.com/fwlink/?LinkId=148640 function from Kernel32.DLL, which frees all bytes so that you can no longer use the memory pointed to by hglobal.

In addition to Marshal.FreeHGlobal(IntPtr), the System.Runtime.InteropServices.Marshal class provides two other memory-deallocation API methods: Marshal.DestroyStructure(IntPtr, Type) and Marshal.FreeCoTaskMem(IntPtr).

Requirements

Namespace: System.Runtime.InteropServices
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0