Re: warning or crash when removing widgets from box




johannes@nada.kth.se (Johannes Keukelaar) writes:

> //In an application I'm writing using Gtk+ (Gtk+ 1.0.4, currently)
> //I have a GtkVBox that is to contain zero or more GtkToggleButtons,
> //which are added and removed from the box according to other user-interface
> //operations.
> //
> //To remove a button, I simply destroy it:
> //	gtk_widget_destroy(vw->button);
> 
> Try gtk_container_remove( GTK_CONTAINER( my_vbox ), vw->button ); instead. This 
> removes it from the vbox, which will cause the vbox to unref it, which will 
> (probably) cause the reference count to reach zero -> destroyed.
> 
> //My understanding is that gdk_widget_destroy detaches the button from its
> //parent container, and then cleans it up.  I thought the above was right,
> //based on an older posting to the gtk-list. 
 
> Erm, no.  It causes it  to be destroyed. Nothing  more. So  the vbox
> will still have a reference to it,  which probably causes the plethora
> of warnings and the eventual  crash. (And then there  is the fact that
> widgets  (like  all objects) are reference   counted,  so you probably
> meant   to say gtk_widget_unref.  But   that's a different story. Read
> docs/refcounting.txt if you're interested.)

Erm, no. ;-) Destroying a widget automatically removes it from it's
parent, which unrefs it, so the original poster was absolutely
correct.

The warnings / segfaults seem to stem from trying to access
a destroyed widget somewhere else in the code. Running the
application under a debugger and breaking on g_warning can
be an effective means of finding out who's passing in the
bad widgets.

                                        Owen



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