[gtk+] GtkListBox: fix model binding refcount issue



commit 6e03e7e80a630c9f7ef3d55130c3abde97ac3012
Author: Ryan Lortie <desrt desrt ca>
Date:   Fri Mar 27 11:57:38 2015 -0400

    GtkListBox: fix model binding refcount issue
    
    As it is, GtkListBox model binding will work nicely as long as your
    create_widget_func returns a floating reference on the newly-created
    widget.
    
    If you try to return a full reference (as any higher-level language
    would do) then you will leak that reference.
    
    Fix that up by converting any floating references into full references
    and then unconditionally releasing the full reference after adding to
    the box.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=746893

 gtk/gtklistbox.c |   12 ++++++++++++
 1 files changed, 12 insertions(+), 0 deletions(-)
---
diff --git a/gtk/gtklistbox.c b/gtk/gtklistbox.c
index b270910..554cd42 100644
--- a/gtk/gtklistbox.c
+++ b/gtk/gtklistbox.c
@@ -3610,9 +3610,21 @@ gtk_list_box_bound_model_changed (GListModel *list,
 
       item = g_list_model_get_item (list, position + i);
       widget = priv->create_widget_func (item, priv->create_widget_func_data);
+
+      /* We allow the create_widget_func to either return a full
+       * reference or a floating reference.  If we got the floating
+       * reference, then turn it into a full reference now.  That means
+       * that gtk_list_box_insert() will take another full reference.
+       * Finally, we'll release this full reference below, leaving only
+       * the one held by the box.
+       */
+      if (g_object_is_floating (widget))
+        g_object_ref_sink (widget);
+
       gtk_widget_show_all (widget);
       gtk_list_box_insert (box, widget, position + i);
 
+      g_object_unref (widget);
       g_object_unref (item);
     }
 }


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