Marshal.FreeHGlobal 方法

释放以前使用 AllocHGlobal 从进程的非托管内存中分配的内存。

**命名空间:**System.Runtime.InteropServices
**程序集:**mscorlib(在 mscorlib.dll 中)

语法

声明
Public Shared Sub FreeHGlobal ( _
    hglobal As IntPtr _
)
用法
Dim hglobal As IntPtr

Marshal.FreeHGlobal(hglobal)
public static void FreeHGlobal (
    IntPtr hglobal
)
public:
static void FreeHGlobal (
    IntPtr hglobal
)
public static void FreeHGlobal (
    IntPtr hglobal
)
public static function FreeHGlobal (
    hglobal : IntPtr
)

参数

  • hglobal
    由对 AllocHGlobal 的原始匹配调用返回的句柄。

备注

可使用 FreeHGlobal 从全局堆释放由 AllocHGlobalReAllocHGlobal 或任何等效非托管 API 方法分配的内存。如果 hglobal 参数为 空引用(在 Visual Basic 中为 Nothing),则此方法什么也不做。

FreeHGlobal 从 Kernel32.DLL 中公开 LocalFree 函数,后者释放所有字节,使您无法再使用 hglobal 所指向的内存。有关 LocalFree 的其他信息,请参见 MSDN Library。

除了 FreeHGlobal 之外,Marshal 类还提供了其他两个内存释放方法:DestroyStructureFreeCoTaskMem

提示

此方法使用 SecurityAction.LinkDemand 防止不可信代码对它进行调用;只有直接调用方才需要具有 SecurityPermissionAttribute.UnmanagedCode 权限。如果您的代码可从部分受信任的代码调用,则未经验证不要将用户输入传递给 Marshal 类方法。有关使用 LinkDemand 成员的重要限制,请参见 Demand 和 LinkDemand

Windows 95, Windows 98, Windows 98 Second Edition, Windows Millennium Edition 平台说明: 将一个无效句柄值传递给 FreeHGlobal 会引发 ArgumentException

示例

下面的代码示例演示如何调用 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);

下面的代码示例演示如何将托管 String 类的内容转换为非托管内存,然后在完成时释放该非托管内存。

.NET Framework 安全性

平台

Windows 98、Windows 2000 SP4、Windows Millennium Edition、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0

请参见

参考

Marshal 类
Marshal 成员
System.Runtime.InteropServices 命名空间
AllocHGlobal
ReAllocHGlobal