[gtk+] inspector: Simplify object life-cycle handling



commit d39ee6c81c4a5fdf38befccbf21d2c4fdf67e3c0
Author: Matthias Clasen <mclasen redhat com>
Date:   Wed May 21 06:39:11 2014 -0400

    inspector: Simplify object life-cycle handling
    
    The prop-list doesn't use the object column in its model,
    so don't put the object there. And don't leak stuff on
    finalize.

 gtk/inspector/prop-list.c  |   58 +++++++++++++++++++++++--------------------
 gtk/inspector/prop-list.ui |   13 ++++-----
 2 files changed, 37 insertions(+), 34 deletions(-)
---
diff --git a/gtk/inspector/prop-list.c b/gtk/inspector/prop-list.c
index 0c9b3b1..3ad8bef 100644
--- a/gtk/inspector/prop-list.c
+++ b/gtk/inspector/prop-list.c
@@ -31,7 +31,6 @@ enum
   COLUMN_NAME,
   COLUMN_VALUE,
   COLUMN_DEFINED_AT,
-  COLUMN_OBJECT,
   COLUMN_TOOLTIP,
   COLUMN_WRITABLE,
   COLUMN_ATTRIBUTE
@@ -182,12 +181,26 @@ row_activated (GtkTreeView *tv,
   g_free (name);
 }
 
+static void cleanup_object (GtkInspectorPropList *pl);
+
+static void
+finalize (GObject *object)
+{
+  GtkInspectorPropList *pl = GTK_INSPECTOR_PROP_LIST (object);
+
+  cleanup_object (pl);
+  g_hash_table_unref (pl->priv->prop_iters);
+
+  G_OBJECT_CLASS (gtk_inspector_prop_list_parent_class)->finalize (object);
+}
+
 static void
 gtk_inspector_prop_list_class_init (GtkInspectorPropListClass *klass)
 {
   GObjectClass *object_class = G_OBJECT_CLASS (klass);
   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
 
+  object_class->finalize = finalize;
   object_class->get_property = get_property;
   object_class->set_property = set_property;
 
@@ -262,7 +275,6 @@ gtk_inspector_prop_list_update_prop (GtkInspectorPropList *pl,
                       COLUMN_NAME, prop->name,
                       COLUMN_VALUE, value ? value : "",
                       COLUMN_DEFINED_AT, g_type_name (prop->owner_type),
-                      COLUMN_OBJECT, pl->priv->object,
                       COLUMN_TOOLTIP, g_param_spec_get_blurb (prop),
                       COLUMN_WRITABLE, (prop->flags & G_PARAM_WRITABLE) != 0,
                       COLUMN_ATTRIBUTE, attribute ? attribute : "",
@@ -278,42 +290,30 @@ gtk_inspector_prop_list_prop_changed_cb (GObject              *pspec,
                                          GParamSpec           *prop,
                                          GtkInspectorPropList *pl)
 {
-  GtkTreeIter *iter = g_hash_table_lookup (pl->priv->prop_iters, prop->name);
+  GtkTreeIter *iter;
+
+  if (!pl->priv->object)
+    return;
 
+  iter = g_hash_table_lookup (pl->priv->prop_iters, prop->name);
   if (iter != NULL)
     gtk_inspector_prop_list_update_prop (pl, iter, prop);
 }
 
-static void remove_dead_object (gpointer data, GObject *dead_object);
-
 static void
 cleanup_object (GtkInspectorPropList *pl)
 {
-  if (pl->priv->object)
-    g_object_weak_unref (pl->priv->object, remove_dead_object, pl);
-
-  if (pl->priv->object && pl->priv->notify_handler_id != 0)
-    {
-      g_signal_handler_disconnect (pl->priv->object, pl->priv->notify_handler_id);
-      pl->priv->notify_handler_id = 0;
-    }
+  if (pl->priv->object &&
+      g_signal_handler_is_connected (pl->priv->object, pl->priv->notify_handler_id))
+    g_signal_handler_disconnect (pl->priv->object, pl->priv->notify_handler_id);
 
   pl->priv->object = NULL;
+  pl->priv->notify_handler_id = 0;
 
   g_hash_table_remove_all (pl->priv->prop_iters);
   gtk_list_store_clear (pl->priv->model);
 }
 
-static void
-remove_dead_object (gpointer data, GObject *dead_object)
-{
-  GtkInspectorPropList *pl = data;
-
-  pl->priv->notify_handler_id = 0;
-  pl->priv->object = NULL;
-  cleanup_object (pl);
-}
-
 gboolean
 gtk_inspector_prop_list_set_object (GtkInspectorPropList *pl,
                                     GObject              *object)
@@ -328,16 +328,12 @@ gtk_inspector_prop_list_set_object (GtkInspectorPropList *pl,
 
   cleanup_object (pl);
 
-  pl->priv->object = object;
-
   if (!object)
     {
       gtk_widget_hide (GTK_WIDGET (pl));
       return TRUE;
     }
 
-  g_object_weak_ref (object, remove_dead_object, pl);
-
   if (pl->priv->child_properties)
     {
       GtkWidget *parent;
@@ -366,6 +362,8 @@ gtk_inspector_prop_list_set_object (GtkInspectorPropList *pl,
       props = g_object_class_list_properties (G_OBJECT_GET_CLASS (object), &num_properties);
     }
 
+  pl->priv->object = object;
+
   for (i = 0; i < num_properties; i++)
     {
       GParamSpec *prop = props[i];
@@ -379,6 +377,11 @@ gtk_inspector_prop_list_set_object (GtkInspectorPropList *pl,
       g_hash_table_insert (pl->priv->prop_iters, (gpointer) prop->name, gtk_tree_iter_copy (&iter));
     }
 
+  g_free (props);
+
+  if (GTK_IS_WIDGET (object))
+    g_signal_connect_swapped (object, "destroy", G_CALLBACK (cleanup_object), pl);
+
   /* Listen for updates */
   pl->priv->notify_handler_id =
       g_signal_connect (object,
@@ -387,6 +390,7 @@ gtk_inspector_prop_list_set_object (GtkInspectorPropList *pl,
                         pl);
 
   gtk_widget_show (GTK_WIDGET (pl));
+
   return TRUE;
 }
 
diff --git a/gtk/inspector/prop-list.ui b/gtk/inspector/prop-list.ui
index 0eade8f..a2f25aa 100644
--- a/gtk/inspector/prop-list.ui
+++ b/gtk/inspector/prop-list.ui
@@ -5,7 +5,6 @@
       <column type="gchararray"/>
       <column type="gchararray"/>
       <column type="gchararray"/>
-      <column type="GObject"/>
       <column type="gchararray"/>
       <column type="gboolean"/>
       <column type="gchararray"/>
@@ -25,7 +24,7 @@
           <object class="GtkTreeView" id="tree">
             <property name="visible">True</property>
             <property name="model">model</property>
-            <property name="tooltip-column">4</property>
+            <property name="tooltip-column">3</property>
             <property name="activate-on-single-click">True</property>
             <signal name="row-activated" handler="row_activated"/>
             <child>
@@ -39,7 +38,7 @@
                   </object>
                   <attributes>
                     <attribute name="text">0</attribute>
-                    <attribute name="sensitive">5</attribute>
+                    <attribute name="sensitive">4</attribute>
                   </attributes>
                 </child>
               </object>
@@ -57,7 +56,7 @@
                   </object>
                   <attributes>
                     <attribute name="text">1</attribute>
-                    <attribute name="sensitive">5</attribute>
+                    <attribute name="sensitive">4</attribute>
                   </attributes>
                 </child>
               </object>
@@ -72,8 +71,8 @@
                     <property name="editable">False</property>
                   </object>
                   <attributes>
-                    <attribute name="text">6</attribute>
-                    <attribute name="sensitive">5</attribute>
+                    <attribute name="text">5</attribute>
+                    <attribute name="sensitive">4</attribute>
                   </attributes>
                 </child>
               </object>
@@ -88,7 +87,7 @@
                   </object>
                   <attributes>
                     <attribute name="text">2</attribute>
-                    <attribute name="sensitive">5</attribute>
+                    <attribute name="sensitive">4</attribute>
                   </attributes>
                 </child>
               </object>


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