Marshal.FreeHGlobal Method (IntPtr)

 

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

Namespace:   System.Runtime.InteropServices
Assembly:  mscorlib (in mscorlib.dll)

[<SecurityCriticalAttribute>]
static member FreeHGlobal : 
        hglobal:nativeint -> unit

Parameters

hglobal
Type: System.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 IntPtr.Zero the method does nothing.

FreeHGlobal exposes the LocalFree function from Kernel32.DLL, which frees all bytes so that you can no longer use the memory pointed to by hglobal.

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

Win95Win98Win98SeWinMe

Passing an invalid handle value to FreeHGlobal causes an ArgumentException.

The following example demonstrates calling the FreeHGlobal method. This code example is part of a larger example provided for the Marshal class.

No code example is currently available or this language may not be supported.

The following example demonstrates how to convert the contents of a managed String class to unmanaged memory and then dispose of the unmanaged memory when done.

No code example is currently available or this language may not be supported.

SecurityCriticalAttribute

requires full trust for the immediate caller. This member cannot be used by partially trusted or transparent code.

Universal Windows Platform
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Windows Phone Silverlight
Available since 8.0
Windows Phone
Available since 8.1
Return to top
Show: