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;

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, it contains no data after being freed:
    String RetrievedString2 = Marshal.PtrToStringAnsi( stringPointer);
    Console.WriteLine("5) RetrievedString2 = " + RetrievedString2 );
    }
}

설명

할당된 AllocHGlobalReAllocHGlobal전역 힙 또는 해당하는 관리되지 않는 API 메서드에서 메모리를 해제하는 데 사용할 FreeHGlobal 수 있습니다. 매개 변수인 hglobal 경우 메서드는 IntPtr.Zero 아무 것도 수행하지 않습니다.

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

또한 FreeHGlobal클래스는 Marshal 두 가지 다른 메모리 할당 취소 API 메서드 DestroyStructureFreeCoTaskMem제공합니다.

적용 대상

추가 정보