Class NativeHandle
A SafeHandle over a native pointer whose release function is supplied at construction, so the handle is always released exactly once even across finalization races.
public sealed class NativeHandle : SafeHandle, IDisposable
- Inheritance
-
NativeHandle
- Implements
- Inherited Members
Remarks
The helper-math capabilities expose a family of handles that differ only in
which *_free function releases them, so one handle type carrying that
function serves them all.
Constructors
NativeHandle(nint, Action<nint>)
Wraps a non-null native pointer with the function that frees it.
public NativeHandle(nint handle, Action<nint> release)
Parameters
handlenintThe pointer returned by a native constructor.
releaseAction<nint>The matching native release function.
Properties
IsInvalid
When overridden in a derived class, gets a value indicating whether the handle value is invalid.
public override bool IsInvalid { get; }
Property Value
Methods
Create(nint, Action<nint>, string)
Creates a wrapper, throwing when the native constructor returned null.
public static NativeHandle Create(nint handle, Action<nint> release, string what)
Parameters
handlenintThe pointer returned by a native constructor.
releaseAction<nint>The matching native release function.
whatstringWhat was being created, for the exception message.
Returns
- NativeHandle
The wrapped handle.
Exceptions
- PamojaException
The native constructor returned null.
ReleaseHandle()
When overridden in a derived class, executes the code required to free the handle.
protected override bool ReleaseHandle()
Returns
- bool
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.
Use(Action<nint>)
Runs a native call that returns nothing, holding the handle open.
public void Use(Action<nint> call)
Parameters
UseTry<TValue>(NativeTry<TValue>)
Runs a native call that answers "maybe", holding the handle open, and maps its bool-plus-out-parameter shape onto a nullable value.
public TValue? UseTry<TValue>(NativeTry<TValue> call) where TValue : struct
Parameters
callNativeTry<TValue>The native call to make.
Returns
- TValue?
The value the call produced, or
nullwhen it produced none.
Type Parameters
TValueThe value the native call may produce.
Use<TResult>(Func<nint, TResult>)
Runs a native call that returns a value, holding the handle open.
public TResult Use<TResult>(Func<nint, TResult> call)
Parameters
Returns
- TResult
Whatever the native call returned.
Type Parameters
TResultThe value the native call returns.