[corlib] Update SafeHandle dispose pattern to match .net. Fixes #25132
authorMarek Safar <marek.safar@gmail.com>
Mon, 8 Dec 2014 16:50:21 +0000 (17:50 +0100)
committerMarek Safar <marek.safar@gmail.com>
Mon, 8 Dec 2014 16:51:38 +0000 (17:51 +0100)
mcs/class/corlib/System.Runtime.InteropServices/SafeHandle.cs

index 52e48cda10ea798161a44672c54fb2eb7214d5e4..51e91c3cb1d411c81ddc69e9e9ed283d3cf21b4a 100644 (file)
@@ -62,7 +62,7 @@ namespace System.Runtime.InteropServices
                //
                protected IntPtr handle;
                IntPtr invalid_handle_value;
-               int refcount = 0;
+               int refcount;
                bool owns_handle;
                
 #if NET_2_1
@@ -75,7 +75,13 @@ namespace System.Runtime.InteropServices
                protected SafeHandle (IntPtr invalidHandleValue, bool ownsHandle)
                {
                        invalid_handle_value = invalidHandleValue;
-                       owns_handle = ownsHandle;
+
+                       if (!ownsHandle) {
+                               GC.SuppressFinalize (this);
+                       } else {
+                               owns_handle = true;
+                       }
+
                        refcount = 1;
                }
 
@@ -198,15 +204,13 @@ namespace System.Runtime.InteropServices
                [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;
+                               }
                        }
                }
 
@@ -233,10 +237,7 @@ namespace System.Runtime.InteropServices
 
                ~SafeHandle ()
                {
-                       if (owns_handle && !IsInvalid){
-                               ReleaseHandle ();
-                               handle = invalid_handle_value;
-                       }
+                       Dispose (false);
                }
        }
 }