[gtksourceview] Increment the ref count of the buffer and release the reference in _dispose()



commit c11cb6dbc7637503571571c8d91fcca4ba6d8b50
Author: Emmanuel Rodriguez <emmanuel rodriguez gmail com>
Date:   Sat Oct 31 12:26:50 2009 +0100

    Increment the ref count of the buffer and release the reference in _dispose()
    
    This patch includes unit tests

 gtksourceview/gtksourceprintcompositor.c |   20 ++++++++++-
 tests/Makefile.am                        |    8 ++++
 tests/test-printcompositor.c             |   54 ++++++++++++++++++++++++++++++
 3 files changed, 80 insertions(+), 2 deletions(-)
---
diff --git a/gtksourceview/gtksourceprintcompositor.c b/gtksourceview/gtksourceprintcompositor.c
index eb55101..489a812 100644
--- a/gtksourceview/gtksourceprintcompositor.c
+++ b/gtksourceview/gtksourceprintcompositor.c
@@ -297,7 +297,7 @@ gtk_source_print_compositor_set_property (GObject      *object,
 	switch (prop_id)
 	{
 		case PROP_BUFFER:
-			compositor->priv->buffer = GTK_SOURCE_BUFFER (g_value_get_object (value));
+			compositor->priv->buffer = GTK_SOURCE_BUFFER (g_value_dup_object (value));
 			break;
 		case PROP_TAB_WIDTH:
 			gtk_source_print_compositor_set_tab_width (compositor,
@@ -376,7 +376,7 @@ gtk_source_print_compositor_finalize (GObject *object)
 
 	if (compositor->priv->footer_font != NULL)
 		pango_font_description_free (compositor->priv->footer_font);
-		
+
 	g_free (compositor->priv->header_format_left);
 	g_free (compositor->priv->header_format_right);
 	g_free (compositor->priv->header_format_center);
@@ -387,6 +387,21 @@ gtk_source_print_compositor_finalize (GObject *object)
 	G_OBJECT_CLASS (gtk_source_print_compositor_parent_class)->finalize (object);
 }
 
+static void
+gtk_source_print_compositor_dispose (GObject *object)
+{
+	GtkSourcePrintCompositor *compositor;
+
+	compositor = GTK_SOURCE_PRINT_COMPOSITOR (object);
+
+	if (compositor->priv->buffer != NULL) {
+		g_object_unref (compositor->priv->buffer);
+		compositor->priv->buffer = NULL;
+	}
+
+	G_OBJECT_CLASS (gtk_source_print_compositor_parent_class)->dispose (object);
+}
+
 static void						 
 gtk_source_print_compositor_class_init (GtkSourcePrintCompositorClass *klass)
 {
@@ -397,6 +412,7 @@ gtk_source_print_compositor_class_init (GtkSourcePrintCompositorClass *klass)
 	object_class->get_property = gtk_source_print_compositor_get_property;
 	object_class->set_property = gtk_source_print_compositor_set_property;
 	object_class->finalize = gtk_source_print_compositor_finalize;
+	object_class->dispose = gtk_source_print_compositor_dispose;
 
 	/**
 	 * GtkSourcePrintCompositor:buffer:
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 3684c3f..61cc842 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -34,6 +34,14 @@ test_languagemanager_LDADD = 		\
 	$(DEP_LIBS)			\
 	$(TESTS_LIBS)
 
+UNIT_TEST_PROGS += test-printcompositor
+test_printcompositor_SOURCES =		\
+	test-printcompositor.c
+test_printcompositor_LDADD = 		\
+	$(top_builddir)/gtksourceview/libgtksourceview-2.0.la \
+	$(DEP_LIBS)			\
+	$(TESTS_LIBS)
+
 # testregion need gtk_text_region_ api which is normally private
 # modify gtksourceview/Makefile.am to export gtk_text_* symbols
 # TEST_PROGS += testregion
diff --git a/tests/test-printcompositor.c b/tests/test-printcompositor.c
new file mode 100644
index 0000000..420462e
--- /dev/null
+++ b/tests/test-printcompositor.c
@@ -0,0 +1,54 @@
+#include "config.h"
+#include <string.h>
+
+#include <gtk/gtk.h>
+#include <gtksourceview/gtksourceprintcompositor.h>
+
+static void
+test_buffer_ref (void)
+{
+	GtkSourcePrintCompositor *compositor;
+	GtkWidget *view = NULL;
+	GtkSourceBuffer *buffer = NULL;
+	GtkSourceBuffer *buffer_original = NULL;
+
+	buffer_original = gtk_source_buffer_new (NULL);
+
+	compositor = gtk_source_print_compositor_new (buffer_original);
+	buffer = gtk_source_print_compositor_get_buffer (compositor);
+	g_assert (GTK_IS_SOURCE_BUFFER (buffer));
+
+	g_object_unref (G_OBJECT (buffer_original));
+	buffer = gtk_source_print_compositor_get_buffer (compositor);
+	g_assert (GTK_IS_SOURCE_BUFFER (buffer));
+}
+
+static void
+test_buffer_view_ref (void)
+{
+	GtkSourcePrintCompositor *compositor;
+	GtkWidget *view = NULL;
+	GtkSourceBuffer *buffer = NULL;
+
+	view = gtk_source_view_new ();
+	compositor = gtk_source_print_compositor_new_from_view (GTK_SOURCE_VIEW (view));
+	buffer = gtk_source_print_compositor_get_buffer (compositor);
+	g_assert (GTK_IS_SOURCE_BUFFER (buffer));
+
+	gtk_widget_destroy (view);
+	buffer = gtk_source_print_compositor_get_buffer (compositor);
+	g_assert (GTK_IS_SOURCE_BUFFER (buffer));
+
+	g_object_unref (G_OBJECT (compositor));
+}
+
+int
+main (int argc, char** argv)
+{
+	gtk_test_init (&argc, &argv);
+
+	g_test_add_func ("/PrintCompositor/buffer-ref", test_buffer_ref);
+	g_test_add_func ("/PrintCompositor/buffer-view-ref", test_buffer_view_ref);
+
+	return g_test_run();
+}



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