cairo: Clean up dispose implementations

This commit is contained in:
Michael Hutchinson 2013-03-05 15:11:46 -05:00 committed by Bertrand Lorentz
parent 99e4257a9f
commit 116d9fcc95
6 changed files with 60 additions and 35 deletions

View File

@ -50,24 +50,27 @@ namespace Cairo
~FontFace () ~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); Dispose (false);
} }
public void Dispose () public void Dispose ()
{ {
Dispose (true); Dispose (true);
GC.SuppressFinalize (this);
} }
protected virtual void Dispose (bool disposing) protected virtual void Dispose (bool disposing)
{ {
if (disposing) if (handle == IntPtr.Zero)
NativeMethods.cairo_font_face_destroy (handle); 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; handle = IntPtr.Zero;
GC.SuppressFinalize (this);
} }
// TODO: make non-public when all entry points are complete in binding // TODO: make non-public when all entry points are complete in binding

View File

@ -33,7 +33,6 @@ namespace Cairo
public class FontOptions : IDisposable public class FontOptions : IDisposable
{ {
IntPtr handle; IntPtr handle;
bool disposed;
public FontOptions () public FontOptions ()
{ {
@ -55,9 +54,10 @@ namespace Cairo
return new FontOptions (NativeMethods.cairo_font_options_copy (handle)); return new FontOptions (NativeMethods.cairo_font_options_copy (handle));
} }
[Obsolete ("Use Dispose()")]
public void Destroy () public void Destroy ()
{ {
NativeMethods.cairo_font_options_destroy (handle); Dispose ();
} }
public void Dispose () public void Dispose ()
@ -66,13 +66,18 @@ namespace Cairo
GC.SuppressFinalize (this); GC.SuppressFinalize (this);
} }
private void Dispose (bool disposing) protected virtual void Dispose (bool disposing)
{ {
if (!disposed) { if (handle == IntPtr.Zero)
Destroy (); return;
handle = IntPtr.Zero;
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) public static bool operator == (FontOptions options, FontOptions other)

View File

@ -36,7 +36,7 @@ namespace Cairo {
public class Path : IDisposable public class Path : IDisposable
{ {
internal IntPtr handle = IntPtr.Zero; internal IntPtr handle = IntPtr.Zero;
internal Path (IntPtr handle) internal Path (IntPtr handle)
{ {
@ -55,9 +55,9 @@ namespace Cairo {
GC.SuppressFinalize (this); GC.SuppressFinalize (this);
} }
protected virtual void Dispose (bool disposing) protected virtual void Dispose (bool disposing)
{ {
if (!disposing){ if (!disposing) {
Console.Error.WriteLine ("Cairo.Path: called from finalization thread, programmer is missing a call to Dispose"); Console.Error.WriteLine ("Cairo.Path: called from finalization thread, programmer is missing a call to Dispose");
return; return;
} }
@ -65,8 +65,8 @@ namespace Cairo {
if (handle == IntPtr.Zero) if (handle == IntPtr.Zero)
return; return;
NativeMethods.cairo_path_destroy (handle); NativeMethods.cairo_path_destroy (handle);
handle = IntPtr.Zero; handle = IntPtr.Zero;
} }
} }
} }

View File

@ -34,7 +34,7 @@ namespace Cairo {
public class Pattern : IDisposable public class Pattern : IDisposable
{ {
protected IntPtr pattern = IntPtr.Zero; protected IntPtr pattern = IntPtr.Zero;
public static Pattern Lookup (IntPtr pattern) public static Pattern Lookup (IntPtr pattern)
{ {
@ -76,6 +76,7 @@ namespace Cairo {
~Pattern () ~Pattern ()
{ {
Dispose (false);
} }
[Obsolete ("Use the SurfacePattern constructor")] [Obsolete ("Use the SurfacePattern constructor")]
@ -92,24 +93,30 @@ namespace Cairo {
public void Dispose () public void Dispose ()
{ {
Dispose (true); Dispose (true);
GC.SuppressFinalize (this);
} }
protected virtual void Dispose (bool disposing) protected virtual void Dispose (bool disposing)
{ {
if (disposing) if (!disposing) {
Destroy (); Console.Error.WriteLine ("Cairo.Pattern: called from finalization thread, programmer is missing a call to Dispose");
GC.SuppressFinalize (this); return;
}
public void Destroy ()
{
if (pattern != IntPtr.Zero){
NativeMethods.cairo_pattern_destroy (pattern);
pattern = IntPtr.Zero;
} }
if (pattern == IntPtr.Zero)
return;
NativeMethods.cairo_pattern_destroy (pattern);
pattern = IntPtr.Zero;
lock (patterns){ lock (patterns){
patterns.Remove (this); patterns.Remove (this);
} }
}
[Obsolete ("Use Dispose()")]
public void Destroy ()
{
Dispose ();
} }
public Extend Extend { public Extend Extend {

View File

@ -99,10 +99,16 @@ namespace Cairo {
protected virtual void Dispose (bool disposing) protected virtual void Dispose (bool disposing)
{ {
if (disposing) { if (handle == IntPtr.Zero)
NativeMethods.cairo_scaled_font_destroy (handle); return;
handle = IntPtr.Zero;
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 () protected void Reference ()

View File

@ -149,6 +149,10 @@ namespace Cairo {
{ {
if (surface == IntPtr.Zero) if (surface == IntPtr.Zero)
return; return;
if (!disposing) {
Console.Error.WriteLine ("Cairo.Surface: called from finalization thread, programmer is missing a call to Dispose");
return;
}
lock (surfaces.SyncRoot) lock (surfaces.SyncRoot)
surfaces.Remove (surface); surfaces.Remove (surface);