Skip to content
This repository was archived by the owner on Oct 1, 2024. It is now read-only.

cairo: fix remaining leaks when finalizer gets called #92

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cairo/Context.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ protected virtual void Dispose (bool disposing)
if (handle == IntPtr.Zero)
return;

NativeMethods.cairo_destroy (handle);
Global.QueueUnref (NativeMethods.cairo_destroy, handle);
handle = IntPtr.Zero;
}

Expand Down
2 changes: 1 addition & 1 deletion cairo/Device.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public Status Acquire ()
public void Dispose ()
{
if (handle != IntPtr.Zero)
NativeMethods.cairo_device_destroy (handle);
Global.QueueUnref (NativeMethods.cairo_device_destroy, handle);
handle = IntPtr.Zero;
GC.SuppressFinalize (this);
}
Expand Down
4 changes: 2 additions & 2 deletions cairo/FontFace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ protected virtual void Dispose (bool disposing)
if (!disposing || CairoDebug.Enabled)
CairoDebug.OnDisposed<FontFace> (handle, disposing);

if (!disposing|| handle == IntPtr.Zero)
if (handle == IntPtr.Zero)
return;

NativeMethods.cairo_font_face_destroy (handle);
Global.QueueUnref (NativeMethods.cairo_font_face_destroy, handle);
handle = IntPtr.Zero;
}

Expand Down
4 changes: 2 additions & 2 deletions cairo/FontOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ protected virtual void Dispose (bool disposing)
if (!disposing || CairoDebug.Enabled)
CairoDebug.OnDisposed<FontOptions> (handle, disposing);

if (!disposing|| handle == IntPtr.Zero)
if (handle == IntPtr.Zero)
return;

NativeMethods.cairo_font_options_destroy (handle);
Global.QueueUnref (NativeMethods.cairo_font_options_destroy, handle);
handle = IntPtr.Zero;
}

Expand Down
44 changes: 44 additions & 0 deletions cairo/Global.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//
// Mono.Cairo.Global.cs
//
// Authors:
// Andrés G. Aragoneses
//
// (C) Andrés G. Aragoneses, 2013.
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

using System;

namespace Cairo
{
internal class Global
{
internal static void QueueUnref (Action<IntPtr> unref_native_function, IntPtr handle)
{
GLib.Timeout.Add (50, () => {
unref_native_function (handle);
return false;
});
}
}
}

2 changes: 2 additions & 0 deletions cairo/Makefile.am
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
ASSEMBLY_NAME = cairo-sharp
ASSEMBLY_NAME_VERSION = $(ASSEMBLY_NAME),Version=$(CAIRO_API_VERSION)
ASSEMBLY = $(ASSEMBLY_NAME).dll
references = -r:$(top_builddir)/glib/glib-sharp.dll
SNK = $(srcdir)/mono.snk

TARGET=$(ASSEMBLY)
Expand Down Expand Up @@ -29,6 +30,7 @@ sources = \
FontType.cs \
FontWeight.cs \
Format.cs \
Global.cs \
GlitzSurface.cs \
Glyph.cs \
Gradient.cs \
Expand Down
2 changes: 1 addition & 1 deletion cairo/NativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -700,7 +700,7 @@ internal static class NativeMethods
internal static extern IntPtr cairo_scaled_font_create (IntPtr fontFace, Matrix matrix, Matrix ctm, IntPtr options);

[DllImport (cairo, CallingConvention=CallingConvention.Cdecl)]
internal static extern IntPtr cairo_scaled_font_destroy (IntPtr scaled_font);
internal static extern void cairo_scaled_font_destroy (IntPtr scaled_font);

[DllImport (cairo, CallingConvention=CallingConvention.Cdecl)]
internal static extern void cairo_scaled_font_extents (IntPtr scaled_font, out FontExtents extents);
Expand Down
4 changes: 2 additions & 2 deletions cairo/Path.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@ protected virtual void Dispose (bool disposing)
if (!disposing || CairoDebug.Enabled)
CairoDebug.OnDisposed<Path> (handle, disposing);

if (!disposing || handle == IntPtr.Zero)
if (handle == IntPtr.Zero)
return;

NativeMethods.cairo_path_destroy (handle);
Global.QueueUnref (NativeMethods.cairo_path_destroy, handle);
handle = IntPtr.Zero;
}
}
Expand Down
4 changes: 2 additions & 2 deletions cairo/Pattern.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,10 @@ protected virtual void Dispose (bool disposing)
if (!disposing || CairoDebug.Enabled)
CairoDebug.OnDisposed<Pattern> (Handle, disposing);

if (!disposing|| Handle == IntPtr.Zero)
if (Handle == IntPtr.Zero)
return;

NativeMethods.cairo_pattern_destroy (Handle);
Global.QueueUnref (NativeMethods.cairo_pattern_destroy, Handle);
Handle = IntPtr.Zero;
}

Expand Down
4 changes: 2 additions & 2 deletions cairo/Region.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,10 @@ protected virtual void Dispose (bool disposing)
if (!disposing || CairoDebug.Enabled)
CairoDebug.OnDisposed<Region> (handle, disposing);

if (!disposing|| handle == IntPtr.Zero)
if (handle == IntPtr.Zero)
return;

NativeMethods.cairo_region_destroy (Handle);
Global.QueueUnref (NativeMethods.cairo_region_destroy, handle);
handle = IntPtr.Zero;
}

Expand Down
4 changes: 2 additions & 2 deletions cairo/ScaledFont.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,10 @@ protected virtual void Dispose (bool disposing)
if (!disposing || CairoDebug.Enabled)
CairoDebug.OnDisposed<ScaledFont> (handle, disposing);

if (!disposing|| handle == IntPtr.Zero)
if (handle == IntPtr.Zero)
return;

NativeMethods.cairo_scaled_font_destroy (handle);
Global.QueueUnref (NativeMethods.cairo_scaled_font_destroy, handle);
handle = IntPtr.Zero;
}

Expand Down
4 changes: 2 additions & 2 deletions cairo/Surface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,10 @@ protected virtual void Dispose (bool disposing)
if (!disposing || CairoDebug.Enabled)
CairoDebug.OnDisposed<Surface> (handle, disposing);

if (!disposing || handle == IntPtr.Zero)
if (handle == IntPtr.Zero)
return;

NativeMethods.cairo_surface_destroy (handle);
Global.QueueUnref (NativeMethods.cairo_surface_destroy, handle);
handle = IntPtr.Zero;
}

Expand Down
7 changes: 7 additions & 0 deletions cairo/cairo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -88,5 +88,12 @@
<Compile Include="Distance.cs" />
<Compile Include="Point.cs" />
<Compile Include="PointD.cs" />
<Compile Include="Global.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\glib\glib.csproj">
<Project>{3BF1D531-8840-4F15-8066-A9788D8C398B}</Project>
<Name>glib</Name>
</ProjectReference>
</ItemGroup>
</Project>