[Mono.Cairo] Clean up Region's Dispose/ctor
authorMichael Hutchinson <m.j.hutchinson@gmail.com>
Wed, 6 Mar 2013 05:35:46 +0000 (00:35 -0500)
committerMichael Hutchinson <m.j.hutchinson@gmail.com>
Wed, 19 Jun 2013 22:37:25 +0000 (18:37 -0400)
mcs/class/Mono.Cairo/Mono.Cairo/Region.cs

index e3b3eb1f83c5c80e896fdcd6a8f6055cdd08ed2d..cd0ba33fde2ba93d694622c675a503bbb65b1d13 100644 (file)
@@ -47,11 +47,7 @@ namespace Cairo
                        get { return handle; }
                }
 
-               ~Region ()
-               {
-                       Console.WriteLine ("Cairo.Region finalizer reached - developer must dispose regions manually to avoid leakage due to thread-safety concerns.");
-               }
-
+               [Obsolete]
                public Region (IntPtr handle) : this (handle, false) {}
 
                public Region (IntPtr handle, bool owned)
@@ -59,11 +55,12 @@ namespace Cairo
                        this.handle = handle;
                        if (!owned)
                                NativeMethods.cairo_region_reference (handle);
+                       if (CairoDebug.Enabled)
+                               CairoDebug.OnAllocated (handle);
                }
 
-               public Region ()
+               public Region () : this (NativeMethods.cairo_region_create () , true)
                {
-                       handle = NativeMethods.cairo_region_create ();
                }
 
                public Region (RectangleInt rect)
@@ -81,14 +78,29 @@ namespace Cairo
                        return new Region (NativeMethods.cairo_region_copy (Handle), true);
                }
 
+               ~Region ()
+               {
+                       Dispose (false);
+               }
+
                public void Dispose ()
                {
-                       if (handle != IntPtr.Zero)
-                               NativeMethods.cairo_region_destroy (Handle);
-                       handle = IntPtr.Zero;
+                       Dispose (true);
                        GC.SuppressFinalize (this);
                }
 
+               protected virtual void Dispose (bool disposing)
+               {
+                       if (!disposing || CairoDebug.Enabled)
+                               CairoDebug.OnDisposed<Region> (handle, disposing);
+
+                       if (!disposing|| handle == IntPtr.Zero)
+                               return;
+
+                       NativeMethods.cairo_region_destroy (Handle);
+                       handle = IntPtr.Zero;
+               }
+
                public override bool Equals (object obj)
                {
                        return (obj is Region) && NativeMethods.cairo_region_equal (Handle, (obj as Region).Handle);