Marshal.FreeHGlobal-Methode: (IntPtr)
Veröffentlicht: Oktober 2016
Gibt Speicherplatz frei, der zuvor aus dem nicht verwalteten Speicher des Prozesses belegt wurde.
Namespace: System.Runtime.InteropServices
Assembly: mscorlib (in mscorlib.dll)
Syntax
[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
)
Parameter
hglobal
Type: System.IntPtrDas Handle, das beim ursprünglichen übereinstimmenden Aufruf von AllocHGlobal zurückgegeben wurde.
Hinweise
Sie können FreeHGlobal zur Freigabe von Speicher aus dem globalen Heap durch AllocHGlobal, ReAllocHGlobal, oder einer gleichwertigen nicht verwalteten API-Methode. Wenn der hglobal -Parameter ist IntPtr.Zero die Methode führt keine Aktion aus.
FreeHGlobal macht die LocalFree -Funktion aus Kernel32.dll verfügbar, die alle Bytes freigibt, sodass Sie den Speicher nicht mehr verwenden, können auf den hglobal.
Zusätzlich zu FreeHGlobal, die Marshal -Klasse bietet zwei andere Speicherfreigabe API-Methoden: DestroyStructure und FreeCoTaskMem.
Win95Win98Win98SeWinMe
Passing an invalid handle value to M:System.Runtime.InteropServices.Marshal.FreeHGlobal(System.IntPtr) causes an T:System.ArgumentException.
Beispiele
Im folgenden Beispiel wird das Aufrufen der FreeHGlobal-Methode veranschaulicht. Dieses Codebeispiel ist Teil eines umfangreicheren Beispiels für die Marshal Klasse.
// 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);
Im folgenden Beispiel wird veranschaulicht, wie der Inhalt eines verwalteten konvertiert String -Klasse in nicht verwalteten Speicher und dann den nicht verwalteten Speicher anschließend verwerfen.
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;
}
Sicherheit
requires full trust for the immediate caller. This member cannot be used by partially trusted or transparent code.
Versionsinformationen
Universelle Windows-Plattform
Verfügbar seit 8
.NET Framework
Verfügbar seit 1.1
Portierbare Klassenbibliothek
Unterstützt in: portierbare .NET-Plattformen
Windows Phone Silverlight
Verfügbar seit 8.0
Windows Phone
Verfügbar seit 8.1
Siehe auch
AllocHGlobal
ReAllocHGlobal
Marshal-Klasse
System.Runtime.InteropServices-Namespace
Zurück zum Anfang