using System;
using System.Runtime.InteropServices;
namespace RenderHookAPI.Hook
{
///
/// Provides a safe handle around a block of unmanaged memory.
///
public class SafeHGlobal : SafeHandle
{
///
/// When overridden in a derived class, gets a value indicating whether the handle value is invalid.
///
/// true if the handle value is invalid; otherwise, false.
public override bool IsInvalid
{
get { return handle == IntPtr.Zero; }
}
///
/// Initializes a new instance of the class.
///
/// The size of the block of memory to allocate, in bytes.
public SafeHGlobal(int sizeInBytes)
: base(Marshal.AllocHGlobal(sizeInBytes), true)
{
}
///
/// When overridden in a derived class, executes the code required to free the handle.
///
///
/// true if the handle is released successfully; otherwise, in the event of a catastrophic failure, false. In this case, it generates a releaseHandleFailed MDA Managed Debugging Assistant.
///
protected override bool ReleaseHandle()
{
Marshal.FreeHGlobal(handle);
return true;
}
}
}