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



> 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...

Ok thanks, the destroy() part was what I was doing wrong.

> >  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, ...);
> >    }
> >  };
> > 
> 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..)

There's still a problem with the dialog.  The first time I make my
gcDialog window, everything works fine.  I allocate it with new, and then
have the ok_callback function delete the widget, like so:
...
   g_print("Before new\n");
   gcDialog *dlg = new gcDialog("Cannot save file");
   g_print("After new\n");
   dlg->add_button("Ok", &ok_callback);
...
   void ok_callback(gcDialog *d)
   {  d->hide(); delete d;  }
...

The first time the dialog pops up, everything works great.  I press ok,
and it goes away without whining at all.  But then when I call this dialog
again, it gives me a blank window (just like before) and the following
error:
   Before new
   ** WARNING **: file gtkbutton.c: line 856 (gtk_button_add):
    "GTK_IS_BUTTON (container)"
   Beginning of CONSTRUCTOR
   After new
(the "Beginning of CONSTRUCTOR" part is a little debug message I put,
quite cleverly, at the beginning of the gcDialog constructor. I'm
assuming the problem occurs in the Gtk_Dialog constructor function.
Should I not be calling this?)

I don't understand why this happens, because I'm creating a completely
different object that doesn't even know about the first.  So why should
the second dialog be broken when the first is fine?
Just in case you need to see my constructor code, it's after my signature.

Thanks again
---
Matt Perry [guy@mikepery.linuxos.org]

If two wrongs don't make a right, try three.
                -- Laurence J. Peter
---

gcDialog::gcDialog(char *titl, char *msg) : Gtk_Dialog()
{
	Gtk_Label *message;

	g_print("Beginning of CONSTRUCTOR\n"); /* error message happens
						  before this */
	this->set_title(titl);
	this->border_width(0);

	message = new Gtk_Label(msg);
	message->set_padding(10, 10);
	this->vbox()->pack_start(message, TRUE, TRUE, 0);
	message->show();

	this->show();
}




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