FreeHGlobal Method
TOC
Collapse the table of content
Expand the table of content

Marshal.FreeHGlobal Method

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

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

public static void FreeHGlobal (
	IntPtr hglobal
)
public static void FreeHGlobal (
	IntPtr hglobal
)
public static function FreeHGlobal (
	hglobal : IntPtr
)
Not applicable.

Parameters

hglobal

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. For additional information about LocalFree, see the MSDN Library.

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

NoteNote:

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.

Windows 95, Windows 98, Windows 98 Second Edition, Windows Millennium Edition Platform Note: Passing an invalid handle value to FreeHGlobal causes an ArgumentException.

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

// Demonstrate how to call GlobalAlloc and 
// GlobalFree using the Marshal class.
IntPtr hglobal = Marshal.AllocHGlobal(100);
Marshal.FreeHGlobal(hglobal);

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

using namespace System;
using namespace System::Runtime::InteropServices;

int main()
{
    // Create a managed string.
    String^ managedString = "Hello unmanaged world (from the managed world).";

    // Marshal the managed string to unmanaged memory.
    char* stringPointer = (char*) Marshal::StringToHGlobalAnsi(managedString ).ToPointer();

    // Always free the unmanaged string.
    Marshal::FreeHGlobal(IntPtr(stringPointer));

    return 0;
}

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0

Community Additions

ADD
Show:
© 2016 Microsoft