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

注釈

を使用FreeHGlobalすると、または同等のアンマネージ API メソッドによってAllocHGlobalReAllocHGlobal割り当てられたグローバル ヒープから任意のメモリを解放できます。 パラメーターが のhglobalIntPtr.Zero場合、 メソッドは何も行いません。

FreeHGlobal は、 localFree 関数を Kernel32.DLL から公開します。これにより、すべてのバイトが解放されるため、 によって hglobal指されるメモリを使用できなくなります。

FreeHGlobal加えて、 Marshal クラスには、メモリ割り当て解除 API メソッドと FreeCoTaskMemという 2 つの他のメソッドが用意されていますDestroyStructure

適用対象

こちらもご覧ください