Merge pull request #2661 from ludovic-henry/fix-safehandle-abort
authormonojenkins <jo.shields+jenkins@xamarin.com>
Mon, 22 Feb 2016 22:25:34 +0000 (22:25 +0000)
committermonojenkins <jo.shields+jenkins@xamarin.com>
Mon, 22 Feb 2016 22:25:34 +0000 (22:25 +0000)
commit9f6238791f8b9e26bf952add917a93045cf07817
tree27be47839ce8b3c1f0bba59b624c8ba212f8c280
parent26053ef5b58b1d4e73e811fa3e046b739267479e
parent942bd2b8b22a8725ed10a31d9e92876d79d704c7
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