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 , ReAllocHGlobal또는 해당하는 관리되지 않는 API 메서드에 의해 AllocHGlobal할당된 전역 힙에서 메모리를 해제할 수 있습니다. 매개 변수가 hglobalIntPtr.Zero 면 메서드는 아무 것도 수행하지 않습니다.

FreeHGlobal 는 Kernel32.DLL LocalFree 함수를 노출하여 에서 가리키는 hglobal메모리를 더 이상 사용할 수 없도록 모든 바이트를 해제합니다.

FreeHGlobal외에도 클래스는 Marshal 및 의 두 가지 다른 메모리 할당 취소 API 메서드 DestroyStructureFreeCoTaskMem를 제공합니다.

적용 대상

추가 정보