//
//
-#if NET_2_0
using System;
using System.Runtime.InteropServices;
using System.Runtime.ConstrainedExecution;
int refcount = 0;
bool owns_handle;
+#if NET_2_1
+ protected SafeHandle ()
+ {
+ throw new NotImplementedException ();
+ }
+#endif
+ [ReliabilityContract (Consistency.WillNotCorruptState, Cer.MayFail)]
protected SafeHandle (IntPtr invalidHandleValue, bool ownsHandle)
{
invalid_handle_value = invalidHandleValue;
refcount = 1;
}
+ [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
public void Close ()
{
if (refcount == 0)
newcount = current-1;
} while (Interlocked.CompareExchange (ref refcount, newcount, current) != current);
- if (newcount == 0 && owns_handle){
+ if (newcount == 0 && owns_handle && !IsInvalid){
ReleaseHandle ();
handle = invalid_handle_value;
+ refcount = -1;
}
}
// try { x.DangerousAddRef (ref release); ... }
// finally { if (release) x.DangerousRelease (); }
//
+ [ReliabilityContract (Consistency.WillNotCorruptState, Cer.MayFail)]
public void DangerousAddRef (ref bool success)
{
- if (refcount == 0)
+ if (refcount <= 0)
throw new ObjectDisposedException (GetType ().FullName);
int newcount, current;
current = refcount;
newcount = current + 1;
- if (handle == invalid_handle_value || current == 0){
+ if (current <= 0){
//
// In MS, calling sf.Close () followed by a call
// to P/Invoke with SafeHandles throws this, but
success = true;
}
+ [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
public IntPtr DangerousGetHandle ()
{
- if (refcount == 0){
+ if (refcount <= 0){
throw new ObjectDisposedException (GetType ().FullName);
}
return handle;
}
+ [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
public void DangerousRelease ()
{
- if (refcount == 0)
+ if (refcount <= 0)
throw new ObjectDisposedException (GetType ().FullName);
int newcount, current;
newcount = current-1;
} while (Interlocked.CompareExchange (ref refcount, newcount, current) != current);
- if (newcount == 0 && owns_handle){
+ if (newcount == 0 && owns_handle && !IsInvalid){
ReleaseHandle ();
handle = invalid_handle_value;
}
}
- public virtual void Dispose ()
+ [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
+ public void Dispose ()
{
Dispose (true);
GC.SuppressFinalize (this);
// See documentation, this invalidates the handle without
// closing it.
//
+ [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
public void SetHandleAsInvalid ()
{
handle = invalid_handle_value;
}
+ [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
protected virtual void Dispose (bool disposing)
{
if (disposing)
}
}
+ [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
protected abstract bool ReleaseHandle ();
+ [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
protected void SetHandle (IntPtr handle)
{
this.handle = handle;
}
public bool IsClosed {
+ [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
get {
- return refcount == 0;
+ return refcount <= 0;
}
}
public abstract bool IsInvalid {
+ [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
get;
}
~SafeHandle ()
{
- if (owns_handle){
+ if (owns_handle && !IsInvalid){
ReleaseHandle ();
handle = invalid_handle_value;
}
}
}
}
-#endif