[gtksourceview/gnome-3-6] Properly handle the buffer weak ref in the gutter renderer
- From: Paolo Borelli <pborelli src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gtksourceview/gnome-3-6] Properly handle the buffer weak ref in the gutter renderer
- Date: Sun, 4 Nov 2012 11:41:42 +0000 (UTC)
commit cfc071998f950ba7c3703833d7e6572027923554
Author: Paolo Borelli <pborelli gnome org>
Date: Sat Nov 3 19:29:56 2012 +0100
Properly handle the buffer weak ref in the gutter renderer
This fixes an invalid write reported by valgrind on close.
gtksourceview/gtksourcegutterrenderer.c | 30 ++++++++++++++++++++++++++++--
1 files changed, 28 insertions(+), 2 deletions(-)
---
diff --git a/gtksourceview/gtksourcegutterrenderer.c b/gtksourceview/gtksourcegutterrenderer.c
index e692f82..dafe17d 100644
--- a/gtksourceview/gtksourcegutterrenderer.c
+++ b/gtksourceview/gtksourcegutterrenderer.c
@@ -82,6 +82,31 @@ enum
PROP_BACKGROUND_SET
};
+/* we cannot simply use g_object_add_weak_pointer
+ * because if the buffer is finalized after the renderer, glib
+ * will try to nullify freed memory, so we need to unref first */
+static void
+set_buffer (GtkSourceGutterRenderer *renderer,
+ GtkTextBuffer *buffer)
+{
+ if (renderer->priv->buffer != NULL)
+ {
+ g_object_weak_unref (G_OBJECT (renderer->priv->buffer),
+ (GWeakNotify) g_nullify_pointer,
+ (gpointer)&renderer->priv->buffer);
+ }
+
+ if (buffer != NULL)
+ {
+ g_object_weak_ref (G_OBJECT (buffer),
+ (GWeakNotify) g_nullify_pointer,
+ (gpointer)&renderer->priv->buffer);
+ }
+
+ renderer->priv->buffer = buffer;
+
+}
+
static void
gtk_source_gutter_renderer_finalize (GObject *object)
{
@@ -104,8 +129,7 @@ emit_buffer_changed (GtkTextView *view,
renderer->priv->buffer);
}
- renderer->priv->buffer = buffer;
- g_object_add_weak_pointer (G_OBJECT (buffer), (gpointer)&renderer->priv->buffer);
+ set_buffer (renderer, buffer);
}
}
@@ -146,6 +170,8 @@ gtk_source_gutter_renderer_dispose (GObject *object)
renderer = GTK_SOURCE_GUTTER_RENDERER (object);
+ set_buffer (renderer, NULL);
+
if (renderer->priv->view)
{
_gtk_source_gutter_renderer_set_view (renderer,
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]