相关文章推荐
发呆的拖把  ·  使用Draggable和DragTarget ...·  12 月前    · 
奔跑的猴子  ·  python - Python ...·  1 年前    · 
重感情的风衣  ·  MongoDB ...·  1 年前    · 
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)

下面的示例演示如何调用 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 从 、 AllocHGlobal ReAllocHGlobal 或任何等效的非托管 API 方法分配的全局堆中释放任何内存。 hglobal 如果 参数为 IntPtr.Zero ,则方法不执行任何工作。

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

除了 FreeHGlobal Marshal 类还提供另外两种内存解除分配 API 方法: DestroyStructure FreeCoTaskMem

即将发布:在整个 2024 年,我们将逐步淘汰作为内容反馈机制的“GitHub 问题”,并将其取代为新的反馈系统。 有关详细信息,请参阅: https://aka.ms/ContentUserFeedback

提交和查看相关反馈