CriticalHandle.ReleaseHandle Method ()

 
System_CAPS_noteNote

The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

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

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

protected abstract bool ReleaseHandle()

Return Value

Type: System.Boolean

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.

The ReleaseHandle method is guaranteed to be called only once, provided that you employ proper synchronization mechanisms to ensure that only one call to the Close or Dispose method is made. The ReleaseHandle method will not be called if the IsInvalid or IsClosed property is true. Implement this method in your CriticalHandle derived classes to execute any code that is required to free the handle. Because one of the functions of CriticalHandle 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 the ReleaseHandle method returns false for any reason, it generates a releaseHandleFailed MDA Managed Debugging Assistant.

Universal Windows Platform
Available since 8
.NET Framework
Available since 2.0
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 8.0
Windows Phone
Available since 8.1
Return to top
Show: