mirror of https://github.com/dahall/Vanara.git
Optimized code on GenericSafeHandle and removed check for invalid on release as this duplicated internal functionality.
parent
aa20a8520a
commit
bf9d17c855
|
@ -14,7 +14,7 @@ namespace Vanara.InteropServices
|
|||
/// <summary>Initializes a new instance of the <see cref="GenericSafeHandle"/> class.</summary>
|
||||
/// <param name="ptr">The pre-existing handle to use.</param>
|
||||
/// <param name="ownsHandle"><see langword="true"/> to reliably release the handle during the finalization phase; <see langword="false"/> to prevent reliable release (not recommended).</param>
|
||||
protected GenericSafeHandle(IntPtr ptr, bool ownsHandle) : base(ownsHandle) { SetHandle(ptr); }
|
||||
protected GenericSafeHandle(IntPtr ptr, bool ownsHandle) : base(ownsHandle) => SetHandle(ptr);
|
||||
|
||||
/// <summary>Initializes a new instance of the <see cref="GenericSafeHandle"/> class.</summary>
|
||||
/// <param name="closeMethod">The delegate method for closing the handle.</param>
|
||||
|
@ -25,21 +25,12 @@ namespace Vanara.InteropServices
|
|||
/// <param name="closeMethod">The delegate method for closing the handle.</param>
|
||||
/// <param name="ownsHandle"><see langword="true"/> to reliably release the handle during the finalization phase; <see langword="false"/> to prevent reliable release (not recommended).</param>
|
||||
/// <exception cref="System.ArgumentNullException">closeMethod</exception>
|
||||
public GenericSafeHandle(IntPtr ptr, Func<IntPtr, bool> closeMethod, bool ownsHandle = true) : base(ownsHandle)
|
||||
{
|
||||
SetHandle(ptr);
|
||||
CloseMethod = closeMethod ?? throw new ArgumentNullException(nameof(closeMethod));
|
||||
}
|
||||
public GenericSafeHandle(IntPtr ptr, Func<IntPtr, bool> closeMethod, bool ownsHandle = true) : base(ownsHandle) => SetHandle(ptr);
|
||||
|
||||
/// <summary>Gets or sets the close method.</summary>
|
||||
/// <value>The close method.</value>
|
||||
protected virtual Func<IntPtr, bool> CloseMethod { get; }
|
||||
|
||||
/// <summary>Performs an explicit conversion from <see cref="GenericSafeHandle"/> to <see cref="IntPtr"/>.</summary>
|
||||
/// <param name="h">The <see cref="GenericSafeHandle"/> instance.</param>
|
||||
/// <returns>The value of the handle. Use caution when using this value as it can be closed by the disposal of the parent <see cref="GenericSafeHandle"/>.</returns>
|
||||
//public static explicit operator IntPtr(GenericSafeHandle h) => h.DangerousGetHandle();
|
||||
|
||||
/// <summary>When overridden in a derived class, executes the code required to free the handle.</summary>
|
||||
/// <returns>
|
||||
/// true if the handle is released successfully; otherwise, in the event of a catastrophic failure, false. In this case, it generates a
|
||||
|
@ -47,7 +38,6 @@ namespace Vanara.InteropServices
|
|||
/// </returns>
|
||||
protected override bool ReleaseHandle()
|
||||
{
|
||||
if (IsInvalid) return true;
|
||||
var ret = CloseMethod?.Invoke(handle) ?? true;
|
||||
SetHandle(IntPtr.Zero);
|
||||
return ret;
|
||||
|
|
Loading…
Reference in New Issue