[goffice] Fixed GOImage references management. [#628732]



commit 0f3d438bb7b724b1ba3bc8589af06611d27b68b9
Author: Jean Brefort <jean brefort normalesup org>
Date:   Sat Sep 4 13:10:37 2010 +0200

    Fixed GOImage references management. [#628732]

 ChangeLog                         |    7 +++++++
 NEWS                              |    1 +
 goffice/app/go-doc.c              |   34 ++++++++++++++++++++++++++++++++--
 goffice/graph/gog-styled-object.c |   15 +++++++++------
 4 files changed, 49 insertions(+), 8 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 26ea953..18d7f88 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2010-09-04  Jean Brefort  <jean brefort normalesup org>
+
+	* goffice/app/go-doc.c (go_doc_init_read), (go_doc_image_fetch): fixed
+	GOImage references management. [#628732]
+	* goffice/graph/gog-styled-object.c
+	(gog_styled_object_document_changed): ditto.
+
 2010-09-03  Jean Brefort  <jean brefort normalesup org>
 
 	reviewed by: <delete if not using a buddy>
diff --git a/NEWS b/NEWS
index a2fe961..5e064b9 100644
--- a/NEWS
+++ b/NEWS
@@ -14,6 +14,7 @@ Jean:
 	* Optionnaly delete trendlines from legend. [#628031]
 	* Fix critical in go_doc_finalize. [#628467]
 	* Fixed a potential crasher in gog_styled_object_document_changed. [#628671]
+	* Fixed GOImage references management. [#628732]
 
 Morten:
 	* Limit formats to sane number of decimals.  [#627066]
diff --git a/goffice/app/go-doc.c b/goffice/app/go-doc.c
index 037981a..f79218b 100644
--- a/goffice/app/go-doc.c
+++ b/goffice/app/go-doc.c
@@ -318,6 +318,13 @@ go_doc_update_meta_data (GODoc *doc)
 	g_signal_emit (G_OBJECT (doc), signals [METADATA_UPDATE], 0);
 }
 
+/** go_doc_get_image:
+ * @doc: a #GODoc
+ * @id: the image name
+ *
+ * Returns: the #GOImage is one exist with name @id. The caller does not own a
+ * reference.
+ */
 GOImage *
 go_doc_get_image (GODoc *doc, char const *id)
 {
@@ -345,6 +352,16 @@ check_for_pixbuf (gpointer key, gpointer img_, gpointer user)
 }
 #endif
 
+/** go_doc_add_image:
+ * @doc: a #GODoc
+ * @id: the image name or NULL
+ * @image: a #GOImage
+ *
+ * Adds @image to the document if no such image already exists. The name of
+ * the returned image might be different from @id, even if given.
+ * Returns: either @image, in which case the document adds a reference on it, or
+ * an identical image for which the owner does not own a reference.
+ */
 GOImage *
 go_doc_add_image (GODoc *doc, char const *id, GOImage *image)
 {
@@ -419,7 +436,7 @@ go_doc_init_read (GODoc *doc, GsfInput *input)
 	g_return_if_fail (doc->imagebuf == NULL);
 
 	doc->imagebuf = g_hash_table_new_full (g_str_hash, g_str_equal,
-					       g_free, NULL);
+					       g_free, g_object_unref);
 	g_object_set_data (G_OBJECT (input), "document", doc);
 }
 
@@ -526,10 +543,23 @@ go_doc_end_read	(GODoc *doc)
 	doc->imagebuf = NULL;
 }
 
+/** go_doc_image_fetch:
+ * @doc: a #GODoc
+ * @id: the name for the new image.
+ *
+ * Searches for a #GOImage with name @id in the document image buffer and
+ * creates one if needed. The caller does not own a reference on the returned
+ * #GOImage.
+ * This function must be called after a call to go_doc_init_read(), otherwise
+ * it will emit a critical and return NULL.
+ * Returns: the found or created #GOImage.
+ */
 GOImage *
 go_doc_image_fetch (GODoc *doc, char const *id)
 {
-	GOImage *image = g_hash_table_lookup (doc->imagebuf, id);
+	GOImage *image;
+	g_return_val_if_fail (doc && doc->imagebuf, NULL);
+	image = g_hash_table_lookup (doc->imagebuf, id);
 	if (!image) {
 		image = g_object_new (GO_TYPE_IMAGE, NULL);
 		go_image_set_name (image, id);
diff --git a/goffice/graph/gog-styled-object.c b/goffice/graph/gog-styled-object.c
index 2121153..8e7dc36 100644
--- a/goffice/graph/gog-styled-object.c
+++ b/goffice/graph/gog-styled-object.c
@@ -63,11 +63,12 @@ gog_styled_object_document_changed (GogObject *obj, GODoc *doc)
 	    (style->fill.type == GO_STYLE_FILL_IMAGE) &&
 	    (style->fill.image.image != NULL)) {
 		GOImage *image;
-		char *id = go_image_get_name (style->fill.image.image);
+		char const *cur_id = go_image_get_name (style->fill.image.image);
+		char *id = NULL;
 		/* remove the (nnn) modifier if any */
-		if (id) {
-			int i = strlen (id) - 1;
-			id = g_strdup (id);
+		if (cur_id) {
+			int i = strlen (cur_id) - 1;
+			id = g_strdup (cur_id);
 			if (id[i] == ')') {
 				i--;
 				while (id[i] >= '0' && id[i] <= '9')
@@ -78,8 +79,10 @@ gog_styled_object_document_changed (GogObject *obj, GODoc *doc)
 		}
 		image = go_doc_add_image (doc, id, style->fill.image.image);
 		g_free (id);
-		g_object_unref (style->fill.image.image);
-		style->fill.image.image = image;
+		if (image != style->fill.image.image) {
+			g_object_unref (style->fill.image.image);
+			style->fill.image.image = g_object_ref (image);
+		}
 	}
 }
 



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