This documentation is archived and is not being maintained.

Marshal.FreeHGlobal Method

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

[Visual Basic]
Public Shared Sub FreeHGlobal( _
   ByVal hglobal As IntPtr _
public static void FreeHGlobal(
 IntPtr hglobal
public: static void FreeHGlobal(
 IntPtr hglobal
public static function FreeHGlobal(
   hglobal : IntPtr


The handle returned by the original matching call to AllocHGlobal.


You can use FreeHGlobal to free any memory from the global heap allocated by AllocHGlobal, ReAllocHGlobal, or any equivalent unmanaged API method. If the hglobal parameter is a null reference (Nothing in Visual Basic), the method does nothing.

FreeHGlobal exposes the GlobalFree function from Kernel32.DLL, which frees all bytes so that you can no longer use the memory pointed to by hglobal. For additional information about GlobalFree, see the MSDN Library.

In addition to FreeHGlobal, the Marshal class provides two other memory-deallocation API methods: DestroyStructure and FreeCoTaskMem.

Note   This method uses SecurityAction.LinkDemand to prevent it from being called from untrusted code; only the immediate caller is required to have SecurityPermissionAttribute.UnmanagedCode permission. If your code can be called from partially trusted code, do not pass user input to Marshal class methods without validation. For important limitations on using the LinkDemand member, see Demand vs. LinkDemand.


Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

.NET Framework Security: 

See Also

Marshal Class | Marshal Members | System.Runtime.InteropServices Namespace | AllocHGlobal | ReAllocHGlobal