Re: destroying an unparented widget



On 05/05/2011 09:37 PM, Allin Cottrell wrote:
There's one thing I've never quite understood: the correct
mechanism for destroying a gtk widget that is never parented or
shown.

The context is that I'm creating a temporary widget with
gtk_label_new(), just for the purpose of getting hold of its
PangoContext so I can figure something out about the font in use
(whether it supports the UTF-8 minus sign, 0x2212). Once I've got
that info I'm using gtk_widget_destroy on the label. And valgrind
is showing me a leak, which I take to mean that I haven't managed
to destroy the widget. If I try using g_object_unref() instead I
get an error message from GTK saying I can't do that on an object
which has only a floating reference.

Would the correct sequence be

label = gtk_label_new("");
g_object_ref_sink(G_OBJECT(label));
/* do pango stuff */
g_object_unref(G_OBJECT(label));

? And if so, is there an equivalent in GLib<  2.10? Thanks.


I think this is the correct way yes. a widget is created with a floating reference. With ref_sink() you now own the reference, and after the unref the reference count should be 0.

you can debug this with the LD_PRELOAD GObject lifetime debugging tool, see http://blogs.gnome.org/danni/2011/02/17/ld_preload-gobject-lifetime-debugging-tool/

Olivier



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