Merge pull request #2661 from ludovic-henry/fix-safehandle-abort
[SafeHandle] Avoid handle leakage in case of ThreadAbortException
If we get a ThreadAbortException while at SafeHandle.cs:124 (before this commit), we would not set release to true, even after incrementing the internal reference count of the SafeHandle. That could lead to potential leak, as the caller (following the common following pattern) would never call DangerousRelase, thus never calling PerformRelease. The finalizer or Dispose would not lead to the call of PerformRelease either, as the DangerousAddRef / DangerousRelase calls are unbalanced.
The common pattern for DangerousAddRef / DangerousRelease is the following:
bool release = false;
try {
safeHandle.DangerousAddRef (ref release);
/* do something with safeHandle.DangerousGetHandle () */
} finally {
if (release)
safeHandle.DangerousRelease ();
}
@monojenkins merge