Re: [gtk-list] Proper way to destroy widgets?



Some Guy <guy@mikepery.pr.mcs.net> writes:
> I was wondering if there was any functions we should call before
> destroying particular widgets.  I'm making an app with a flexible-ish
> dialog class (coded in GTK--).  The class is derived from Gtk_Dialog, and
> when it is initialized, it creates a new label and slaps that in the vbox,
> then you can add buttons which it slaps into the action_area.  Here's some
> pseudo-code to clarify:

gtk-- handles destroying things alittle differently from gtk. 
1) you should never call destroy(), as it'll only delete the C-part of
   the widget. (== actually it'll only turn the widget unusable)
2) if you allocate something with new, only thing to delete it, is to
   first hide() it and then delete the C++ object. (you should use
   destructors etc to do deletion..)
3) you can put objects to local or class scope as they are and they'll
   be cleaned up properly (as far as its not in screen when you delete it).
4) Never do delete this. When you feel need for this, add idle handler and
   pass pointer to the widget in extra arg and deletee the widget in the
   idle handler...

So, main rule is: 
   Use widgets just like you'd use normal C++ objects, if you allocate it with
   new, you MUST delete it yourself..

(there's currently one problem with window manager close button as gtk
wants to delete the C part automatically... That way you might get
black screens too next time... deriving from the widget and overriding
delete_event_impl and reeturning correct value from it fixes that
currently, but for some reason connected callbacks you cannot do that
:( thats a bug in gtk-- that should be fixed...) (=> might force you
to derive from a widget just to override delete_event_impl)

>  class gcDialog : Gtk_Dialog {
>    gcDialog(char *label_txt) {
>      Gtk_Label *label = new Gtk_Label(label_txt);
>      vbox().pack_start(label, ...);
>    }
>    add_button(char *label_txt, FuncPtr *func) {
>      Gtk_Button *button = new Gtk_Button();
>      action_area().pack_start(button, ...);
>    }
>  };
> 
> Do I need to preserve copies of the child widgets (the labels and buttons)
> in order to free them when the dialog is destroyed?  What other functions
> should I call to get the dialog window off the screen?  Currently I have
> my button callbacks call dialog->hide() and dialog->destroy(), but for
> some reason it gives me the following errors when a button is pressed:
> 
> ** WARNING **: invalid cast from (NULL) pointer to `GtkObject'
> 
> ** WARNING **: file gtkobject.c: line 1092 (gtk_object_get_data_by_id):
> "object != NULL"
> 
> ** WARNING **: file gtkbutton.c: line 856 (gtk_button_add): "GTK_IS_BUTTON
> (container)"
> 
> and the next time I open a dialog, it gives me a blank window.

Yes, It did destroy only the C part.. and then you afterwards try to use the
C++ widget... (for commonly used dialogs, plain hide()/show() pair could be
enough and keep the widget in mem..)

-- 
-- Tero Pulkkinen -- terop@modeemi.cs.tut.fi --



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