[devhelp] Fix ownership of DhLinks in DhBase



commit ef5dc3bf2d019659300c58459b592487633edf76
Author: Jonathon Jongsma <jonathon quotidian org>
Date:   Tue Sep 29 17:23:30 2009 -0500

    Fix ownership of DhLinks in DhBase
    
    Previously there were two collections of lists in DhBase: priv->keywords, and
    priv->book_tree.  Neither of these lists had clear ownership of the DhLinks that
    they held.  This patch adds a ref to the DhLink before adding it to each list,
    and then unrefs the items from each list when destroying DhBase, in order to
    prevent leaking of all of these objects.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=596808

 src/dh-base.c   |   16 ++++++++++++++++
 src/dh-parser.c |    5 +++--
 2 files changed, 19 insertions(+), 2 deletions(-)
---
diff --git a/src/dh-base.c b/src/dh-base.c
index ca33a2c..707e320 100644
--- a/src/dh-base.c
+++ b/src/dh-base.c
@@ -67,6 +67,13 @@ static void base_add_xcode_docsets (DhBase      *base,
 static DhBase *base_instance;
 
 static void
+unref_node_link (GNode *node, gpointer data)
+{
+    dh_link_unref (node->data);
+    dh_link_unref (node->data);
+}
+
+static void
 base_finalize (GObject *object)
 {
         DhBasePriv *priv;
@@ -75,8 +82,17 @@ base_finalize (GObject *object)
 
         /* FIXME: Free things... */
         g_hash_table_destroy (priv->books);
+        g_node_traverse (priv->book_tree,
+                         G_IN_ORDER,
+                         G_TRAVERSE_ALL,
+                         -1,
+                         (GNodeTraverseFunc)unref_node_link,
+                         NULL);
         g_node_destroy (priv->book_tree);
 
+        g_list_foreach (priv->keywords, (GFunc)dh_link_unref, NULL);
+        g_list_free (priv->keywords);
+
         G_OBJECT_CLASS (dh_base_parent_class)->finalize (object);
 }
 
diff --git a/src/dh-parser.c b/src/dh-parser.c
index 226d11d..99f95d9 100644
--- a/src/dh-parser.c
+++ b/src/dh-parser.c
@@ -157,12 +157,13 @@ parser_start_node_book (DhParser             *parser,
                             uri);
         g_free (base);
 
-        *parser->keywords = g_list_prepend (*parser->keywords, link);
+        *parser->keywords = g_list_prepend (*parser->keywords, dh_link_ref (link));
 
-        parser->book_node = g_node_new (link);
+        parser->book_node = g_node_new (dh_link_ref (link));
         g_node_prepend (parser->book_tree, parser->book_node);
         parser->parent = parser->book_node;
         g_free (title);
+        dh_link_unref (link);
 }
 
 static void



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