New tests.
[mono.git] / mcs / class / corlib / System.Runtime.InteropServices / SafeHandle.cs
index e3824ac56203c0162b60930e3a15d1de9a63b788..e1b0a70ffb24ddc408c4b20af7f1c0b81ae2814f 100644 (file)
@@ -43,7 +43,6 @@
 //
 //
 
-#if NET_2_0
 using System;
 using System.Runtime.InteropServices;
 using System.Runtime.ConstrainedExecution;
@@ -62,6 +61,12 @@ namespace System.Runtime.InteropServices
                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)
                {
@@ -85,6 +90,7 @@ namespace System.Runtime.InteropServices
                        if (newcount == 0 && owns_handle && !IsInvalid){
                                ReleaseHandle ();
                                handle = invalid_handle_value;
+                               refcount = -1;
                        }
                }
 
@@ -100,7 +106,7 @@ namespace System.Runtime.InteropServices
                [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;
@@ -108,7 +114,7 @@ namespace System.Runtime.InteropServices
                                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
@@ -124,7 +130,7 @@ namespace System.Runtime.InteropServices
                [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
                public IntPtr DangerousGetHandle ()
                {
-                       if (refcount == 0){
+                       if (refcount <= 0){
                                throw new ObjectDisposedException (GetType ().FullName);
                        }
 
@@ -134,7 +140,7 @@ namespace System.Runtime.InteropServices
                [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
                public void DangerousRelease ()
                {
-                       if (refcount == 0)
+                       if (refcount <= 0)
                                throw new ObjectDisposedException (GetType ().FullName);
 
                        int newcount, current;
@@ -150,7 +156,7 @@ namespace System.Runtime.InteropServices
                }
 
                [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
-               public virtual void Dispose ()
+               public void Dispose ()
                {
                        Dispose (true);
                        GC.SuppressFinalize (this);
@@ -193,7 +199,7 @@ namespace System.Runtime.InteropServices
                public bool IsClosed {
                        [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
                        get {
-                               return refcount == 0;
+                               return refcount <= 0;
                        }
                }
 
@@ -211,4 +217,3 @@ namespace System.Runtime.InteropServices
                }
        }
 }
-#endif