Marshal.FreeHGlobal(IntPtr) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
释放以前从进程的非托管内存中分配的内存。
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);
}
}
注解
可以使用 FreeHGlobal 从 、 AllocHGlobalReAllocHGlobal或任何等效的非托管 API 方法分配的全局堆中释放任何内存。
hglobal
如果 参数为 IntPtr.Zero ,则方法不执行任何工作。
FreeHGlobal 从 Kernel32.DLL 公开 LocalFree 函数,该函数释放所有字节,以便不再使用 指向的 hglobal
内存。
除了 FreeHGlobal之外, Marshal 类还提供另外两个内存解除分配 API 方法: DestroyStructure 和 FreeCoTaskMem。