Export (0) Print
Expand All

SafeHandle.ReleaseHandle Method

Note: This method is new in the .NET Framework version 2.0.

When overridden in a derived class, executes the code required to free the handle.

Namespace: System.Runtime.InteropServices
Assembly: mscorlib (in mscorlib.dll)

protected:
virtual bool ReleaseHandle () abstract
protected abstract boolean ReleaseHandle ()
protected abstract function ReleaseHandle () : boolean

Return Value

true if the handle is released successfully; otherwise, in the event of a catastrophic failure, false. In this case, it generates a ReleaseHandleFailed Managed Debugging Assistant.

The ReleaseHandle method is guaranteed to be called only once and only if the handle is valid as defined by the IsInvalid property. Implement this method in your SafeHandle derived classes to execute any code that is required to free the handle. Because one of the functions of SafeHandle is to guarantee prevention of resource leaks, the code in your implementation of ReleaseHandle must never fail. The garbage collector calls ReleaseHandle after normal finalizers have been run for objects that were garbage collected at the same time, and guarantees the resources to invoke it and that it will not be interrupted while it is in progress. This method will be prepared as a constrained execution region (CER) at instance construction time (along with all the methods in its statically determinable call graph). Although this prevents thread abort interrupts, you must still be careful not to introduce any fault paths in your overridden ReleaseHandle method. In particular, apply the ReliabilityContractAttribute attribute to any methods you call from ReleaseHandle. In most cases this code should be:

ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)

Additionally, for simple cleanup (for example, calling the Win32 API CloseHandle on a file handle) you can check the return value for the single platform invoke call. For complex cleanup, you may have a lot of program logic and many method calls, some of which might fail. You must ensure that your program logic has fallback code for each of those cases.

If ReleaseHandle returns false for any reason, it generates a ReleaseHandleFailed Managed Debugging Assistant. This helps you detect cases where your attempt to release resources fails.

The following code example demonstrates how to implement the SafeHandle class.

[DllImport("kernel32")]
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
private static extern bool CloseHandle(IntPtr handle);

protected override bool ReleaseHandle()
{
    return CloseHandle(handle);
}

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

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0

Community Additions

ADD
Show:
© 2014 Microsoft