SafeHandle Class

Definition

Represents a wrapper class for operating system handles. This class must be inherited.

[SecurityCritical]
public abstract class SafeHandle : CriticalFinalizerObject, IDisposable
Inheritance
SafeHandle
Derived
Attributes

Inherited Members

System.Object

Constructors

SafeHandle(IntPtr, Boolean)

Initializes a new instance of the SafeHandle class with the specified invalid handle value.

protected SafeHandle(IntPtr invalidHandleValue, bool ownsHandle)
Parameters
invalidHandleValue
IntPtr

The value of an invalid handle (usually 0 or -1). Your implementation of IsInvalid should return true for this value.

ownsHandle
Boolean

true to reliably let SafeHandle release the handle during the finalization phase; otherwise, false (not recommended).

Attributes
Exceptions

The derived class resides in an assembly without unmanaged code access permission.

Fields

handle

Specifies the handle to be wrapped.

protected IntPtr handle
Attributes

Properties

IsClosed

Gets a value indicating whether the handle is closed.

public bool IsClosed { get; }
Value
Boolean

true if the handle is closed; otherwise, false.

Attributes

IsInvalid

When overridden in a derived class, gets a value indicating whether the handle value is invalid.

public abstract bool IsInvalid { get; }
Value
Boolean

true if the handle value is invalid; otherwise, false.

Attributes

Methods

DangerousAddRef(ref Boolean)

Manually increments the reference counter on SafeHandle instances.

[SecurityCritical]
public void DangerousAddRef(ref bool success)
Parameters
success
Boolean

true if the reference counter was successfully incremented; otherwise, false.

Attributes
See Also

DangerousGetHandle()

Returns the value of the handle field.

public IntPtr DangerousGetHandle()
Returns

An IntPtr representing the value of the handle field. If the handle has been marked invalid with SetHandleAsInvalid(), this method still returns the original handle value, which can be a stale value.

Attributes

DangerousRelease()

Manually decrements the reference counter on a SafeHandle instance.

[SecurityCritical]
public void DangerousRelease()
Attributes
See Also

Dispose()

Releases all resources used by the SafeHandle class.

public void Dispose()
Attributes

Dispose(Boolean)

Releases the unmanaged resources used by the SafeHandle class specifying whether to perform a normal dispose operation.

[SecurityCritical]
protected virtual void Dispose(bool disposing)
Parameters
disposing
Boolean

true for a normal dispose operation; false to finalize the handle.

Attributes

Finalize()

Frees all resources associated with the handle.

protected void Finalize()
Attributes
See Also

ReleaseHandle()

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

protected abstract bool ReleaseHandle()
Returns

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.

Attributes

SetHandle(IntPtr)

Sets the handle to the specified pre-existing handle.

protected void SetHandle(IntPtr handle)
Parameters
handle
IntPtr

The pre-existing handle to use.

Attributes

SetHandleAsInvalid()

Marks a handle as no longer used.

[SecurityCritical]
public void SetHandleAsInvalid()
Attributes