//
protected IntPtr handle;
IntPtr invalid_handle_value;
- int refcount = 0;
+ int refcount;
bool owns_handle;
#if NET_2_1
protected SafeHandle (IntPtr invalidHandleValue, bool ownsHandle)
{
invalid_handle_value = invalidHandleValue;
- owns_handle = ownsHandle;
+
+ if (!ownsHandle) {
+ GC.SuppressFinalize (this);
+ } else {
+ owns_handle = true;
+ }
+
refcount = 1;
}
[ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
protected virtual void Dispose (bool disposing)
{
- if (disposing)
+ if (disposing) {
Close ();
- else {
- //
- // The docs say `never call this with disposing=false',
- // the question is whether:
- // * The runtime will ever call Dipose(false) for SafeHandles (special runtime case)
- // * Whether we should just call ReleaseHandle regardless?
- //
+ } else {
+ if (owns_handle && !IsInvalid){
+ ReleaseHandle ();
+ handle = invalid_handle_value;
+ }
}
}
~SafeHandle ()
{
- if (owns_handle && !IsInvalid){
- ReleaseHandle ();
- handle = invalid_handle_value;
- }
+ Dispose (false);
}
}
}