[gtk+/wip/cssnode3: 14/81] cssnode: Implement refcounting



commit 7915a60e850725b63a0f76cec79e0245abb3caef
Author: Benjamin Otte <otte redhat com>
Date:   Mon Jan 26 06:13:01 2015 +0100

    cssnode: Implement refcounting
    
    The parent refs the child, so gtk_css_node_set_parent() adds/removes a
    reference.
    
    We should probably refactor this so that we name the function
    parent.add(node) instead of node.set_parent(parent) - makes the
    refcounting more clear.

 gtk/gtkcssnode.c      |   22 ++++++++++++++++++++++
 gtk/gtkstylecontext.c |    2 ++
 2 files changed, 24 insertions(+), 0 deletions(-)
---
diff --git a/gtk/gtkcssnode.c b/gtk/gtkcssnode.c
index 4e33010..fd4c88f 100644
--- a/gtk/gtkcssnode.c
+++ b/gtk/gtkcssnode.c
@@ -24,6 +24,19 @@
 G_DEFINE_TYPE (GtkCssNode, gtk_css_node, G_TYPE_OBJECT)
 
 static void
+gtk_css_node_dispose (GObject *object)
+{
+  GtkCssNode *cssnode = GTK_CSS_NODE (object);
+
+  while (cssnode->first_child)
+    {
+      gtk_css_node_set_parent (cssnode->first_child, NULL);
+    }
+
+  G_OBJECT_CLASS (gtk_css_node_parent_class)->dispose (object);
+}
+
+static void
 gtk_css_node_finalize (GObject *object)
 {
   GtkCssNode *cssnode = GTK_CSS_NODE (object);
@@ -58,6 +71,7 @@ gtk_css_node_class_init (GtkCssNodeClass *klass)
 {
   GObjectClass *object_class = G_OBJECT_CLASS (klass);
 
+  object_class->dispose = gtk_css_node_dispose;
   object_class->finalize = gtk_css_node_finalize;
 
   klass->invalidate = gtk_css_node_real_invalidate;
@@ -78,6 +92,9 @@ gtk_css_node_set_parent (GtkCssNode *node,
   if (node->parent == parent)
     return;
 
+  /* Take a reference here so the whole function has a reference */
+  g_object_ref (node);
+
   if (node->parent != NULL)
     {
       if (!GTK_IS_CSS_TRANSIENT_NODE (node))
@@ -98,6 +115,8 @@ gtk_css_node_set_parent (GtkCssNode *node,
       node->parent = NULL;
       node->next_sibling = NULL;
       node->previous_sibling = NULL;
+
+      g_object_unref (node);
     }
 
   if (parent)
@@ -121,6 +140,9 @@ gtk_css_node_set_parent (GtkCssNode *node,
     }
 
   gtk_css_node_invalidate (node, GTK_CSS_CHANGE_ANY_PARENT | GTK_CSS_CHANGE_ANY_SIBLING);
+
+  if (node->parent == NULL)
+    g_object_unref (node);
 }
 
 GtkCssNode *
diff --git a/gtk/gtkstylecontext.c b/gtk/gtkstylecontext.c
index 434e977..b585033 100644
--- a/gtk/gtkstylecontext.c
+++ b/gtk/gtkstylecontext.c
@@ -305,6 +305,8 @@ gtk_style_context_pop_style_node (GtkStyleContext *context)
 
   g_return_if_fail (priv->saved_nodes != NULL);
 
+  if (GTK_IS_CSS_TRANSIENT_NODE (priv->cssnode))
+    gtk_css_node_set_parent (priv->cssnode, NULL);
   g_object_unref (priv->cssnode);
   priv->cssnode = priv->saved_nodes->data;
   priv->saved_nodes = g_slist_remove (priv->saved_nodes, priv->cssnode);


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