[PATCH] Properly destroy the cairo context when we're done with it

Linus Torvalds torvalds at linux-foundation.org
Tue Jan 29 16:55:08 PST 2013


From: Linus Torvalds <torvalds at linux-foundation.org>
Date: Wed, 30 Jan 2013 11:50:26 +1100
Subject: [PATCH] Properly destroy the cairo context when we're done with it

Henrik reports that on OSX, not destroying the cairo context results in
a corrupted profile display.  It seems to just leak memory on Linux.

For gtk3, the cairo context is embedded in the GtkDrawable, for gtk2 we
need to create and destroy it appropriately.  Although maybe we could
just make it static instead of creating/destroying it all the time.

Anyway, this goes back to the old cairo destroy logic for gtk2.

Reported-and-analyzed-by: Henrik Brautaset Aronsen <subsurface at henrik.synth.no>
Signed-off-by: Linus Torvalds <torvalds at linux-foundation.org>
---
 gtk-gui.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)

diff --git a/gtk-gui.c b/gtk-gui.c
index 2df816675798..8cb5c47e31eb 100644
--- a/gtk-gui.c
+++ b/gtk-gui.c
@@ -1692,7 +1692,7 @@ static gboolean profile_tooltip (GtkWidget *widget, gint x, gint y,
 static double zoom_factor = 1.0;
 static int zoom_x = -1, zoom_y = -1;
 
-static gboolean common_drawing_function(GtkWidget *widget, struct graphics_context *gc)
+static void common_drawing_function(GtkWidget *widget, struct graphics_context *gc)
 {
 	int i = 0;
 	struct dive *dive = current_dive;
@@ -1724,8 +1724,6 @@ static gboolean common_drawing_function(GtkWidget *widget, struct graphics_conte
 		tooltips = 0;
 		plot(gc, dive, SC_SCREEN);
 	}
-
-	return FALSE;
 }
 
 #if GTK_CHECK_VERSION(3,0,0)
@@ -1742,7 +1740,8 @@ static gboolean draw_callback(GtkWidget *widget, cairo_t *cr, gpointer data)
 	gc.drawing_area.height = height;
 	gc.cr = cr;
 
-	return common_drawing_function(widget, &gc);
+	common_drawing_function(widget, &gc);
+	return FALSE;
 }
 
 #else /* gtk2 */
@@ -1759,7 +1758,9 @@ static gboolean expose_event(GtkWidget *widget, GdkEventExpose *event, gpointer
 	gc.drawing_area.height = allocation.height;
 	gc.cr = gdk_cairo_create(gtk_widget_get_window(widget));
 
-	return common_drawing_function(widget, &gc);
+	common_drawing_function(widget, &gc);
+	cairo_destroy(gc.cr);
+	return FALSE;
 }
 
 #endif
-- 
1.8.1.2.422.g08c0e7f



More information about the subsurface mailing list