[goffice] Canvas: use dispose methods to avoid ref cycles.



commit b463f4333e7a33879a8aff68653baef2bc320467
Author: Morten Welinder <terra gnome org>
Date:   Mon Sep 14 09:35:05 2009 -0400

    Canvas: use dispose methods to avoid ref cycles.

 ChangeLog                   |   17 +++++++++++++++++
 goffice/canvas/goc-canvas.c |   11 ++++++++++-
 goffice/canvas/goc-group.c  |   25 ++++++++++++++-----------
 goffice/canvas/goc-group.h  |    1 +
 goffice/canvas/goc-item.c   |   18 ++++++++++++++----
 goffice/canvas/goc-item.h   |    1 +
 goffice/canvas/goc-widget.c |    2 ++
 7 files changed, 59 insertions(+), 16 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 53b47dc..7f26c39 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,20 @@
+2009-09-13  Morten Welinder  <terra gnome org>
+
+	* goffice/canvas/goc-widget.c (goc_widget_get_property): Add
+	missing "break".
+
+	* goffice/canvas/goc-item.c (goc_item_dispose): Rename from
+	goc_item_finalize.
+	(goc_item_destroy): New function.
+
+	* goffice/canvas/goc-group.c (goc_group_clear): New function.
+	(goc_group_dispose): Renamed from goc_group_finalize and changed
+	to simply call goc_group_clear.
+	(goc_group_class_init): Hook-up ::dispose instead of finalize.
+
+	* goffice/canvas/goc-canvas.c (goc_canvas_dispose): New handler to
+	get rid of items.
+
 2009-09-13  Jean Brefort  <jean brefort normalesup org>
 
 	* docs/reference/goffice-0.8-sections.txt: more API doc work.
diff --git a/goffice/canvas/goc-canvas.c b/goffice/canvas/goc-canvas.c
index 1091d0f..db8e21c 100644
--- a/goffice/canvas/goc-canvas.c
+++ b/goffice/canvas/goc-canvas.c
@@ -228,7 +228,15 @@ goc_canvas_finalize (GObject *obj)
 {
 	GocCanvas *canvas = GOC_CANVAS (obj);
 	g_object_unref (G_OBJECT (canvas->root));
-	(parent_klass->finalize) (obj);
+	parent_klass->finalize (obj);
+}
+
+static void
+goc_canvas_dispose (GObject *obj)
+{
+	GocCanvas *canvas = GOC_CANVAS (obj);
+	goc_group_clear (canvas->root);
+	parent_klass->dispose (obj);
 }
 
 static void
@@ -236,6 +244,7 @@ goc_canvas_class_init (GObjectClass *klass)
 {
 	parent_klass = g_type_class_peek_parent (klass);
 	klass->finalize = goc_canvas_finalize;
+	klass->dispose = goc_canvas_dispose;
 }
 
 static void
diff --git a/goffice/canvas/goc-group.c b/goffice/canvas/goc-group.c
index 6e0e5e7..0b0bf2d 100644
--- a/goffice/canvas/goc-group.c
+++ b/goffice/canvas/goc-group.c
@@ -219,18 +219,11 @@ goc_group_notify_scrolled (GocItem *item)
 }
 
 static void
-goc_group_finalize (GObject *obj)
+goc_group_dispose (GObject *obj)
 {
 	GocGroup *group = GOC_GROUP (obj);
-	GList *l = g_list_first (group->children);
-	while (l) {
-		GOC_ITEM (l->data)->parent = NULL;
-		g_object_unref (G_OBJECT (l->data));
-		l = g_list_next (l);
-	}
-	g_list_free (group->children);
-	group->children = NULL;
-	(parent_klass->finalize) (obj);
+	goc_group_clear (group);
+	parent_klass->dispose (obj);
 }
 
 static void
@@ -241,7 +234,7 @@ goc_group_class_init (GocItemClass *item_klass)
 
 	obj_klass->get_property = goc_group_get_property;
 	obj_klass->set_property = goc_group_set_property;
-	obj_klass->finalize = goc_group_finalize;
+	obj_klass->dispose = goc_group_dispose;
 	g_object_class_install_property (obj_klass, GROUP_PROP_X,
 		g_param_spec_double ("x",
 			_("x"),
@@ -283,6 +276,16 @@ goc_group_new (GocGroup *parent)
 }
 
 void
+goc_group_clear (GocGroup *group)
+{
+	g_return_if_fail (GOC_IS_GROUP (group));
+	while (group->children != NULL) {
+		GocItem *child = group->children->data;
+		goc_item_destroy (child);
+	}
+}
+
+void
 goc_group_add_child (GocGroup *group, GocItem *item)
 {
 	g_return_if_fail (GOC_IS_GROUP (group));
diff --git a/goffice/canvas/goc-group.h b/goffice/canvas/goc-group.h
index b3c8833..d6fa244 100644
--- a/goffice/canvas/goc-group.h
+++ b/goffice/canvas/goc-group.h
@@ -47,6 +47,7 @@ struct _GocGroupClass
 GType goc_group_get_type (void);
 
 GocGroup *goc_group_new (GocGroup *parent);
+void goc_group_clear (GocGroup *group);
 void goc_group_add_child (GocGroup *parent, GocItem *item);
 void goc_group_remove_child (GocGroup *parent, GocItem *item);
 void goc_group_adjust_bounds (GocGroup const *group, double *x0, double *y0, double *x1, double *y1);
diff --git a/goffice/canvas/goc-item.c b/goffice/canvas/goc-item.c
index 511aad2..74b8dfd 100644
--- a/goffice/canvas/goc-item.c
+++ b/goffice/canvas/goc-item.c
@@ -101,7 +101,7 @@ goc_item_leave_notify (GocItem *item, double x, double y)
 }
 
 static void
-goc_item_finalize (GObject *object)
+goc_item_dispose (GObject *object)
 {
 	GocItem *item = GOC_ITEM (object);
 	if (item->canvas) {
@@ -112,9 +112,11 @@ goc_item_finalize (GObject *object)
 			goc_item_invalidate (item);
 		}
 	}
+
 	if (item->parent != NULL)
 		goc_group_remove_child (item->parent, item);
-	item_parent_class->finalize (object);
+
+	item_parent_class->dispose (object);
 }
 
 static void
@@ -130,7 +132,7 @@ goc_item_class_init (GocItemClass *item_klass)
 	item_klass->enter_notify = goc_item_enter_notify;
 	item_klass->leave_notify = goc_item_leave_notify;
 
-	obj_klass->finalize = goc_item_finalize;
+	obj_klass->dispose = goc_item_dispose;
 }
 
 static void
@@ -171,6 +173,13 @@ goc_item_new (GocGroup *group, GType type, const gchar *first_arg_name, ...)
 }
 
 void
+goc_item_destroy (GocItem *item)
+{
+	g_object_run_dispose (G_OBJECT (item));
+	g_object_unref (item);
+}
+
+void
 goc_item_set (GocItem *item, const gchar *first_arg_name, ...)
 {
 	va_list args;
@@ -239,7 +248,8 @@ goc_item_move (GocItem *item, double x, double y)
 		klass->move (item, x, y);
 }
 
-void goc_item_invalidate (GocItem *item)
+void
+goc_item_invalidate (GocItem *item)
 {
 	GocGroup const *parent;
 	double x0, y0, x1, y1;
diff --git a/goffice/canvas/goc-item.h b/goffice/canvas/goc-item.h
index 2980af6..1e8f63f 100644
--- a/goffice/canvas/goc-item.h
+++ b/goffice/canvas/goc-item.h
@@ -74,6 +74,7 @@ struct _GocItemClass {
 GType goc_item_get_type (void);
 
 GocItem		*goc_item_new		(GocGroup *parent, GType type, const gchar *first_arg_name, ...);
+void		 goc_item_destroy	(GocItem *item);
 void		 goc_item_set		(GocItem *item, const gchar *first_arg_name, ...);
 double		 goc_item_distance	(GocItem *item, double x, double y, GocItem **near_item);
 void		 goc_item_draw		(GocItem const *item, cairo_t *cr);
diff --git a/goffice/canvas/goc-widget.c b/goffice/canvas/goc-widget.c
index af105d0..b769e8d 100644
--- a/goffice/canvas/goc-widget.c
+++ b/goffice/canvas/goc-widget.c
@@ -149,6 +149,8 @@ goc_widget_get_property (GObject *obj, guint param_id,
 	switch (param_id) {
 	case WIDGET_PROP_WIDGET:
 		g_value_set_object (value, item->widget);
+		break;
+
 	case WIDGET_PROP_X:
 		g_value_set_double (value, item->x);
 		break;



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