[gtksourceview/gtksourcecompletion] Correctly dispose buffers



commit ac28324f1c0623e31904b7b43637f7f24fa49342
Author: Jesse van den Kieboom <jessevdk gnome org>
Date:   Mon Sep 21 23:07:50 2009 +0200

    Correctly dispose buffers

 .../words/gtksourcecompletionwords.c               |   61 +++++++++++++-------
 .../words/gtksourcecompletionwords.h               |   14 ++--
 .../words/gtksourcecompletionwordsbuffer.c         |   22 ++++----
 3 files changed, 59 insertions(+), 38 deletions(-)
---
diff --git a/gtksourceview/completion-providers/words/gtksourcecompletionwords.c b/gtksourceview/completion-providers/words/gtksourcecompletionwords.c
index 9f16ee1..f23b5cf 100644
--- a/gtksourceview/completion-providers/words/gtksourcecompletionwords.c
+++ b/gtksourceview/completion-providers/words/gtksourcecompletionwords.c
@@ -28,7 +28,7 @@
 #include <gtksourceview/gtksourceview-i18n.h>
 #include <string.h>
 
-#define GTK_SOURCE_COMPLETION_WORDS_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE((object), GSC_TYPE_PROVIDER_WORDS, GtkSourceCompletionWordsPrivate))
+#define GTK_SOURCE_COMPLETION_WORDS_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE((object), GTK_TYPE_SOURCE_COMPLETION_WORDS, GtkSourceCompletionWordsPrivate))
 
 #define BUFFER_KEY "GtkSourceCompletionWordsBufferKey"
 
@@ -68,6 +68,12 @@ typedef struct
 	GObjectClass parent_class;
 } GscProposalWordsClass;
 
+typedef struct
+{
+	GtkSourceCompletionWords *words;
+	GtkSourceCompletionWordsBuffer *buffer;
+} BufferBinding;
+
 static void gtk_source_completion_words_iface_init (GtkSourceCompletionProviderIface *iface);
 
 GType gsc_proposal_words_get_type (void);
@@ -269,22 +275,25 @@ gtk_source_completion_words_populate (GtkSourceCompletionProvider *provider,
 }
 
 static void
-remove_buffer (GtkSourceCompletionWordsBuffer *buffer)
+remove_buffer (BufferBinding *binding)
 {
-	g_object_set_data (G_OBJECT (gtk_source_completion_words_buffer_get_buffer (buffer)),
+	g_object_set_data (G_OBJECT (gtk_source_completion_words_buffer_get_buffer (binding->buffer)),
 	                   BUFFER_KEY,
 	                   NULL);
-
-	g_object_unref (buffer);
 }
 
 static void
 gtk_source_completion_words_dispose (GObject *object)
 {
 	GtkSourceCompletionWords *provider = GTK_SOURCE_COMPLETION_WORDS (object);
+	GList *cp;
+	
 	population_finished (provider);
 	
-	g_list_foreach (provider->priv->buffers, (GFunc)remove_buffer, NULL);
+	cp = g_list_copy (provider->priv->buffers);
+	g_list_foreach (cp, (GFunc)remove_buffer, NULL);
+	
+	g_list_free (cp);
 	g_list_free (provider->priv->buffers);
 	
 	g_free (provider->priv->name);
@@ -456,24 +465,34 @@ GtkSourceCompletionWords *
 gtk_source_completion_words_new (gchar const *name,
                                  GdkPixbuf   *icon)
 {
-	return g_object_new (GSC_TYPE_PROVIDER_WORDS, 
+	return g_object_new (GTK_TYPE_SOURCE_COMPLETION_WORDS, 
 	                     "name", name, 
 	                     "icon", icon, 
 	                     NULL);
 }
 
+static void
+buffer_destroyed (BufferBinding *binding)
+{
+	binding->words->priv->buffers = g_list_remove (binding->words->priv->buffers, 
+	                                               binding);
+	g_object_unref (binding->buffer);
+	g_slice_free (BufferBinding, binding);
+}
+
 void
 gtk_source_completion_words_register (GtkSourceCompletionWords *words,
                                       GtkTextBuffer            *buffer)
 {
 	GtkSourceCompletionWordsBuffer *buf;
+	BufferBinding *binding;
 
 	g_return_if_fail (GTK_IS_SOURCE_COMPLETION_WORDS (words));
 	g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
 	
-	buf = g_object_get_data (G_OBJECT (buffer), BUFFER_KEY);
+	binding = g_object_get_data (G_OBJECT (buffer), BUFFER_KEY);
 	
-	if (buf != NULL)
+	if (binding != NULL)
 	{
 		return;
 	}
@@ -482,25 +501,27 @@ gtk_source_completion_words_register (GtkSourceCompletionWords *words,
 	                                              buffer,
 	                                              words->priv->scan_batch_size);
 
-	g_object_set_data (G_OBJECT (buf), BUFFER_KEY, buffer);
-	words->priv->buffers = g_list_prepend (words->priv->buffers, buf);
+	
+	binding = g_slice_new (BufferBinding);
+	binding->words = words;
+	binding->buffer = buf;
+	
+	g_object_set_data_full (G_OBJECT (buffer), 
+	                        BUFFER_KEY, 
+	                        binding,
+	                        (GDestroyNotify)buffer_destroyed);
+
+	words->priv->buffers = g_list_prepend (words->priv->buffers, 
+	                                       binding);
 }
 
 void
 gtk_source_completion_words_unregister (GtkSourceCompletionWords *words,
                                         GtkTextBuffer            *buffer)
 {
-	GtkSourceCompletionWordsBuffer *buf;
-	
 	g_return_if_fail (GTK_IS_SOURCE_COMPLETION_WORDS (words));
 	g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
 	
-	buf = g_object_get_data (G_OBJECT (buffer), BUFFER_KEY);
-	
-	if (buf != NULL)
-	{
-		remove_buffer (buf);
-		words->priv->buffers = g_list_remove (words->priv->buffers, buf);
-	}
+	g_object_set_data (G_OBJECT (buffer), BUFFER_KEY, NULL);
 }
 
diff --git a/gtksourceview/completion-providers/words/gtksourcecompletionwords.h b/gtksourceview/completion-providers/words/gtksourcecompletionwords.h
index 02de97a..5529342 100644
--- a/gtksourceview/completion-providers/words/gtksourcecompletionwords.h
+++ b/gtksourceview/completion-providers/words/gtksourcecompletionwords.h
@@ -28,13 +28,13 @@
 
 G_BEGIN_DECLS
 
-#define GSC_TYPE_PROVIDER_WORDS				(gtk_source_completion_words_get_type ())
-#define GTK_SOURCE_COMPLETION_WORDS(obj)		(G_TYPE_CHECK_INSTANCE_CAST ((obj), GSC_TYPE_PROVIDER_WORDS, GtkSourceCompletionWords))
-#define GTK_SOURCE_COMPLETION_WORDS_CONST(obj)		(G_TYPE_CHECK_INSTANCE_CAST ((obj), GSC_TYPE_PROVIDER_WORDS, GtkSourceCompletionWords const))
-#define GTK_SOURCE_COMPLETION_WORDS_CLASS(klass)	(G_TYPE_CHECK_CLASS_CAST ((klass), GSC_TYPE_PROVIDER_WORDS, GtkSourceCompletionWordsClass))
-#define GTK_IS_SOURCE_COMPLETION_WORDS(obj)		(G_TYPE_CHECK_INSTANCE_TYPE ((obj), GSC_TYPE_PROVIDER_WORDS))
-#define GTK_IS_SOURCE_COMPLETION_WORDS_CLASS(klass)	(G_TYPE_CHECK_CLASS_TYPE ((klass), GSC_TYPE_PROVIDER_WORDS))
-#define GTK_SOURCE_COMPLETION_WORDS_GET_CLASS(obj)	(G_TYPE_INSTANCE_GET_CLASS ((obj), GSC_TYPE_PROVIDER_WORDS, GtkSourceCompletionWordsClass))
+#define GTK_TYPE_SOURCE_COMPLETION_WORDS				(gtk_source_completion_words_get_type ())
+#define GTK_SOURCE_COMPLETION_WORDS(obj)		(G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_SOURCE_COMPLETION_WORDS, GtkSourceCompletionWords))
+#define GTK_SOURCE_COMPLETION_WORDS_CONST(obj)		(G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_SOURCE_COMPLETION_WORDS, GtkSourceCompletionWords const))
+#define GTK_SOURCE_COMPLETION_WORDS_CLASS(klass)	(G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_SOURCE_COMPLETION_WORDS, GtkSourceCompletionWordsClass))
+#define GTK_IS_SOURCE_COMPLETION_WORDS(obj)		(G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_SOURCE_COMPLETION_WORDS))
+#define GTK_IS_SOURCE_COMPLETION_WORDS_CLASS(klass)	(G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_SOURCE_COMPLETION_WORDS))
+#define GTK_SOURCE_COMPLETION_WORDS_GET_CLASS(obj)	(G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_SOURCE_COMPLETION_WORDS, GtkSourceCompletionWordsClass))
 
 typedef struct _GtkSourceCompletionWords		GtkSourceCompletionWords;
 typedef struct _GtkSourceCompletionWordsClass		GtkSourceCompletionWordsClass;
diff --git a/gtksourceview/completion-providers/words/gtksourcecompletionwordsbuffer.c b/gtksourceview/completion-providers/words/gtksourcecompletionwordsbuffer.c
index 17ba908..3e84182 100644
--- a/gtksourceview/completion-providers/words/gtksourcecompletionwordsbuffer.c
+++ b/gtksourceview/completion-providers/words/gtksourcecompletionwordsbuffer.c
@@ -101,17 +101,6 @@ gtk_source_completion_words_buffer_dispose (GObject *object)
 	GtkSourceCompletionWordsBuffer *buffer =
 			GTK_SOURCE_COMPLETION_WORDS_BUFFER (object);
 
-	if (buffer->priv->library)
-	{
-		g_signal_handler_disconnect (buffer->priv->library,
-		                             buffer->priv->lock_handler_id);
-		g_signal_handler_disconnect (buffer->priv->library,
-		                             buffer->priv->unlock_handler_id);
-
-		g_object_unref (buffer->priv->library);
-		buffer->priv->library = NULL;
-	}
-	
 	if (buffer->priv->buffer)
 	{
 		gint i;
@@ -142,6 +131,17 @@ gtk_source_completion_words_buffer_dispose (GObject *object)
 
 	buffer->priv->lines = NULL;
 
+	if (buffer->priv->library)
+	{
+		g_signal_handler_disconnect (buffer->priv->library,
+		                             buffer->priv->lock_handler_id);
+		g_signal_handler_disconnect (buffer->priv->library,
+		                             buffer->priv->unlock_handler_id);
+
+		g_object_unref (buffer->priv->library);
+		buffer->priv->library = NULL;
+	}
+
 	G_OBJECT_CLASS (gtk_source_completion_words_buffer_parent_class)->dispose (object);
 }
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]