Método Marshal.FreeHGlobal (IntPtr)
Dica
The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.
Libera memória anteriormente alocada da memória não gerenciada do processo.
Namespace: System.Runtime.InteropServices
Assembly: mscorlib (em mscorlib.dll)
Sintaxe
[SecurityCriticalAttribute]
public static void FreeHGlobal(
IntPtr hglobal
)
public:
[SecurityCriticalAttribute]
static void FreeHGlobal(
IntPtr hglobal
)
[<SecurityCriticalAttribute>]
static member FreeHGlobal :
hglobal:nativeint -> unit
<SecurityCriticalAttribute>
Public Shared Sub FreeHGlobal (
hglobal As IntPtr
)
Parâmetros
hglobal
Type: System.IntPtrO identificador retornado pela chamada original correspondente para AllocHGlobal.
Comentários
Você pode usar FreeHGlobal para liberar qualquer memória do heap global alocada por AllocHGlobal, ReAllocHGlobal, ou nenhum equivalente de método API não gerenciado. Se o hglobal parâmetro é IntPtr.Zero o método não fará nada.
FreeHGlobalexpõe o LocalFree função Kernel32. dll, que libera todos os bytes, para que você não pode usar a memória apontado pela hglobal.
Além FreeHGlobal, o Marshal classe fornece dois outros desalocação de memória métodos de API: DestroyStructure e FreeCoTaskMem.
Win95Win98Win98SeWinMe
Passing an invalid handle value to M:System.Runtime.InteropServices.Marshal.FreeHGlobal(System.IntPtr) causes an T:System.ArgumentException.
Exemplos
O exemplo a seguir demonstra a chamar o FreeHGlobal método. Este exemplo de código é parte de um exemplo maior fornecido para a Marshal classe.
// 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)
// Demonstrate how to call GlobalAlloc and
// GlobalFree using the Marshal class.
IntPtr hglobal = Marshal::AllocHGlobal(100);
Marshal::FreeHGlobal(hglobal);
O exemplo a seguir demonstra como converter o conteúdo de um gerenciado String classe a memória não gerenciada e, em seguida, descarte a memória não gerenciada quando terminar.
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 );
}
}
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;
}
Segurança
requires full trust for the immediate caller. This member cannot be used by partially trusted or transparent code.
Informações de Versão
Plataforma Universal do Windows
Disponível desde 8
.NET Framework
Disponível desde 1.1
Biblioteca de Classes Portátil
Com suporte no: plataformas portáteis do .NET
Windows Phone Silverlight
Disponível desde 8.0
Windows Phone
Disponível desde 8.1
Confira Também
AllocHGlobal
ReAllocHGlobal
Classe Marshal
Namespace System.Runtime.InteropServices
Retornar ao início