[Mono.Cairo] Clean up dispose implementations
authorMichael Hutchinson <m.j.hutchinson@gmail.com>
Tue, 5 Mar 2013 20:11:46 +0000 (15:11 -0500)
committerMichael Hutchinson <m.j.hutchinson@gmail.com>
Wed, 19 Jun 2013 22:35:47 +0000 (18:35 -0400)
mcs/class/Mono.Cairo/Mono.Cairo/FontFace.cs
mcs/class/Mono.Cairo/Mono.Cairo/FontOptions.cs
mcs/class/Mono.Cairo/Mono.Cairo/Path.cs
mcs/class/Mono.Cairo/Mono.Cairo/Pattern.cs
mcs/class/Mono.Cairo/Mono.Cairo/ScaledFont.cs
mcs/class/Mono.Cairo/Mono.Cairo/Surface.cs

index efa479b6d52dfd756d49abc84f23ca939ea892c6..b24e9dfb1bd91d97c34fe32d12fb9a4d80807b24 100644 (file)
@@ -50,24 +50,27 @@ namespace Cairo
 
                ~FontFace ()
                {
-                       // Since Cairo is not thread safe, we can not unref the
-                       // font_face here, the programmer must do this with Dispose
-
-                       Console.Error.WriteLine ("Programmer forgot to call Dispose on the FontFace");
                        Dispose (false);
                }
 
                public void Dispose ()
                {
                        Dispose (true);
+                       GC.SuppressFinalize (this);
                }
 
                protected virtual void Dispose (bool disposing)
                {
-                       if (disposing)
-                               NativeMethods.cairo_font_face_destroy (handle);
+                       if (handle == IntPtr.Zero)
+                               return;
+
+                       if (!disposing) {
+                               Console.Error.WriteLine ("Cairo.FontFace: called from finalization thread, programmer is missing a call to Dispose");
+                               return;
+                       }
+
+                       NativeMethods.cairo_font_face_destroy (handle);
                        handle = IntPtr.Zero;
-                       GC.SuppressFinalize (this);
                }
                
                // TODO: make non-public when all entry points are complete in binding
index 476910f1b1d35396394f8c83430a0366139aa88f..fa3e84f094bedd2897a40e2c629c85db82c67b20 100644 (file)
@@ -33,7 +33,6 @@ namespace Cairo
        public class FontOptions : IDisposable
        {
                IntPtr handle;
-               bool disposed;
 
                public FontOptions ()
                {
@@ -55,9 +54,10 @@ namespace Cairo
                        return new FontOptions (NativeMethods.cairo_font_options_copy (handle));
                }
 
+               [Obsolete ("Use Dispose()")]
                public void Destroy ()
                {
-                       NativeMethods.cairo_font_options_destroy (handle);
+                       Dispose ();
                }
 
                public void Dispose ()
@@ -66,13 +66,18 @@ namespace Cairo
                        GC.SuppressFinalize (this);
                }
 
-               private void Dispose (bool disposing)
+               protected virtual void Dispose (bool disposing)
                {
-                       if (!disposed) {
-                               Destroy ();
-                               handle = IntPtr.Zero;
+                       if (handle == IntPtr.Zero)
+                               return;
+
+                       if (!disposing) {
+                               Console.Error.WriteLine ("Cairo.FontOptions: called from finalization thread, programmer is missing a call to Dispose");
+                               return;
                        }
-                       disposed = true;
+
+                       NativeMethods.cairo_font_options_destroy (handle);
+                       handle = IntPtr.Zero;
                }
 
                public static bool operator == (FontOptions options, FontOptions other)
index 97cfb8e2862f1ed297ce5af26a63520ab9d6a89a..f3dbff648c12a16d9bb7a67f206260439a2ea4dc 100644 (file)
@@ -36,7 +36,7 @@ namespace Cairo {
 
         public class Path : IDisposable 
         {
-                internal IntPtr handle = IntPtr.Zero;
+               internal IntPtr handle = IntPtr.Zero;
                
                internal Path (IntPtr handle)
                {
@@ -57,8 +57,8 @@ namespace Cairo {
                
                 protected virtual void Dispose (bool disposing)
                 {
-                       if (!disposing){
-                               Console.Error.WriteLine ("Cairo.Context: called from finalization thread, programmer is missing a call to Dispose");
+                       if (!disposing) {
+                               Console.Error.WriteLine ("Cairo.Path: called from finalization thread, programmer is missing a call to Dispose");
                                return;
                        }
                        
index ebe4b70eaf5f3e8b3585f1f17240753d0d21acc5..7bf97bd170416f65498dce4aeccab7240b4185fb 100644 (file)
@@ -34,7 +34,7 @@ namespace Cairo {
    
         public class Pattern : IDisposable
         {
-                protected IntPtr pattern = IntPtr.Zero;
+               protected IntPtr pattern = IntPtr.Zero;
 
                internal static Pattern Lookup (IntPtr pattern)
                {
@@ -76,6 +76,7 @@ namespace Cairo {
 
                ~Pattern ()
                {
+                       Dispose (false);
                }
                
                 [Obsolete ("Use the SurfacePattern constructor")]
@@ -92,24 +93,30 @@ namespace Cairo {
                public void Dispose ()
                {
                        Dispose (true);
+                       GC.SuppressFinalize (this);
                }
 
                protected virtual void Dispose (bool disposing)
                {
-                       if (disposing)
-                               Destroy ();
-                       GC.SuppressFinalize (this);
-               }
-               
-                public void Destroy ()
-                {
-                       if (pattern != IntPtr.Zero){
-                               NativeMethods.cairo_pattern_destroy (pattern);
-                               pattern = IntPtr.Zero;
+                       if (!disposing) {
+                               Console.Error.WriteLine ("Cairo.Pattern: called from finalization thread, programmer is missing a call to Dispose");
+                               return;
                        }
+
+                       if (pattern == IntPtr.Zero)
+                               return;
+
+                       NativeMethods.cairo_pattern_destroy (pattern);
+                       pattern = IntPtr.Zero;
                        lock (patterns){
                                patterns.Remove (this);
                        }
+               }
+
+               [Obsolete ("Use Dispose()")]
+                public void Destroy ()
+                {
+                       Dispose ();
                 }
                
                public Status Status
index 2b0f6cf0c5fb5a8a6cb9f223e4022b815741e458..a15958dae9158ddcb434fa65e55bc6bbb135fdf2 100644 (file)
@@ -99,10 +99,16 @@ namespace Cairo {
 
                protected virtual void Dispose (bool disposing)
                {
-                       if (disposing) {
-                               NativeMethods.cairo_scaled_font_destroy (handle);
-                               handle = IntPtr.Zero;
+                       if (handle == IntPtr.Zero)
+                               return;
+
+                       if (!disposing) {
+                               Console.Error.WriteLine ("Cairo.ScaledFont: called from finalization thread, programmer is missing a call to Dispose");
+                               return;
                        }
+
+                       NativeMethods.cairo_scaled_font_destroy (handle);
+                       handle = IntPtr.Zero;
                }
                
                 protected void Reference ()
index 862cbdc606d85ac92f2556106359da1aa97ae3f7..977019a169ff8213bd172de4d824efd1973d894d 100644 (file)
@@ -149,6 +149,10 @@ namespace Cairo {
                {
                        if (surface == IntPtr.Zero)
                                return;
+                       if (!disposing) {
+                               Console.Error.WriteLine ("Cairo.Surface: called from finalization thread, programmer is missing a call to Dispose");
+                               return;
+                       }
                        
                        lock (surfaces.SyncRoot)
                                surfaces.Remove (surface);