Marshal.FreeHGlobal(IntPtr) Metodo

Definizione

Libera la memoria precedentemente allocata dalla memoria non gestita del processo.Frees memory previously allocated from the unmanaged memory of the process.

public:
 static void FreeHGlobal(IntPtr hglobal);
[System.Security.SecurityCritical]
public static void FreeHGlobal (IntPtr hglobal);
public static void FreeHGlobal (IntPtr hglobal);
static member FreeHGlobal : nativeint -> unit
Public Shared Sub FreeHGlobal (hglobal As IntPtr)

Parametri

hglobal
IntPtr

L'handle restituito dalla chiamata di corrispondenza originale a AllocHGlobal(IntPtr).The handle returned by the original matching call to AllocHGlobal(IntPtr).

Attributi

Esempio

Nel codice riportato di seguito viene illustrata la chiamata al metodo FreeHGlobal.The following example demonstrates calling the FreeHGlobal method. Questo esempio di codice fa parte di un esempio più ampio fornito per la classe Marshal.This code example is part of a larger example provided for the Marshal class.

// 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)

Nell'esempio seguente viene illustrato come convertire il contenuto di una classe String gestita nella memoria non gestita e quindi eliminare la memoria non gestita al termine dell'operazione.The following example demonstrates how to convert the contents of a managed String class to unmanaged memory and then dispose of the unmanaged memory when done.

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

Commenti

È possibile usare FreeHGlobal per liberare memoria dall'heap globale allocato da AllocHGlobal, ReAllocHGlobalo qualsiasi metodo API non gestito equivalente.You can use FreeHGlobal to free any memory from the global heap allocated by AllocHGlobal, ReAllocHGlobal, or any equivalent unmanaged API method. Se il parametro hglobal è IntPtr.Zero il metodo non esegue alcuna operazione.If the hglobal parameter is IntPtr.Zero the method does nothing.

FreeHGlobal espone la funzione LocalFree da Kernel32. dll, che libera tutti i byte in modo da non poter più usare la memoria a cui punta hglobal.FreeHGlobal exposes the LocalFree function from Kernel32.DLL, which frees all bytes so that you can no longer use the memory pointed to by hglobal.

Oltre a FreeHGlobal, la classe Marshal fornisce altri due metodi API per la deallocazione della memoria: DestroyStructure e FreeCoTaskMem.In addition to FreeHGlobal, the Marshal class provides two other memory-deallocation API methods: DestroyStructure and FreeCoTaskMem.

Si applica a

Vedi anche