Marshal.FreeHGlobal(IntPtr) 方法

定义

释放以前从进程的非托管内存中分配的内存。

public:
 static void FreeHGlobal(IntPtr hglobal);
[System.Security.SecurityCritical]
public static void FreeHGlobal (IntPtr hglobal);
public static void FreeHGlobal (IntPtr hglobal);
[<System.Security.SecurityCritical>]
static member FreeHGlobal : nativeint -> unit
static member FreeHGlobal : nativeint -> unit
Public Shared Sub FreeHGlobal (hglobal As IntPtr)

参数

hglobal
IntPtr

nativeint

原始匹配调用 AllocHGlobal(IntPtr)返回的句柄。

属性

示例

以下示例演示如何调用 FreeHGlobal 方法。 此代码示例是为 Marshal 类提供的大型示例的一部分。

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

以下示例演示如何将托管 String 类的内容转换为非托管内存,然后在完成后释放非托管内存。

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

#include <iostream>                                                 // for printf


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();

    printf("stringPointer = %s\n", stringPointer);

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

    return 0;
}
using System;
using System.Runtime.InteropServices;
using System.Threading;

class MainFunction
{
    static void Main()
    {
        Console.WriteLine("\nStringToGlobalAnsi\n");

        // Create a managed string.
        String  managedString = "I am a managed String";
        Console.WriteLine("1) managedString = " + managedString);

        // Marshal the managed string to unmanaged memory.
        IntPtr stringPointer = (IntPtr)Marshal.StringToHGlobalAnsi(managedString);
        Console.WriteLine("2) stringPointer = {0}", stringPointer);

        // Get the string back from unmanaged memory.
        String RetrievedString = Marshal.PtrToStringAnsi(stringPointer);
        Console.WriteLine("3) Retrieved from unmanaged memory = " + RetrievedString);

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

        // IntPtr handle value is still the same:
        Console.WriteLine("4) stringPointer = " + stringPointer);

        // However, the data may be cleared after the memory is freed, depending on whether the memory allocated to stringPointer
        // has been reclaimed or not. Uncommenting the following line (Thread.Sleep(1000)) increases the likelihood of the memory being reclaimed.
        // Thread.Sleep(1000);
        String RetrievedString2 = Marshal.PtrToStringAnsi(stringPointer);
        Console.WriteLine("5) RetrievedString2 = " + RetrievedString2);
    }
}

注解

重要

此本机内存分配器是一个旧版 API,应在 Windows 平台上由特定 Win32 API 调用时专门使用。 面向 .NET 6 或更高版本时,请使用所有平台上的 NativeMemory 类来分配本机内存。 面向 .NET 6 或更早版本时,在所有平台上使用 AllocCoTaskMem 来分配本机内存。

可以使用 FreeHGlobal 从由 AllocHGlobalReAllocHGlobal或任何等效的非托管 API 方法分配的全局堆中释放任何内存。 如果 hglobal 参数 IntPtr.Zero 该方法不执行任何操作。

FreeHGlobal 从 Kernel32.DLL 公开 LocalFree 函数,该函数释放所有字节,以便你不能再使用 hglobal指向的内存。

除了 FreeHGlobalMarshal 类还提供另外两种内存解除分配 API 方法:DestroyStructureFreeCoTaskMem

适用于

另请参阅