Re: Calling gtk_widget_destroy()



This is interesting. I understand what you are saying. What I find
interesting is that gtk_widget_destroy(window) doesn't segfault when you
close via the window manager. 

I am no GTK expert but it is my understanding that gtk_main_quit will go
through all the widgets that currently exist and destroy them. Someone
correct me if I am wrong but I suspect it goes through all the widget's
that have a reference count > 0 and calls gtk_widget_destroy or a
similar internal function. 

Now maybe it performs differently when it fires the delete event because
refcount has allready been dropped to 0 so gtk_main_quit doesn't null
the pointer as it sees the object as allready deleted. Hence
gtk_widget_destroy doesn't segfault. Perhaps if an object is deleted
with the delete_event the pointer isn't set to null whereas if its
deleted in gtk_main_quit it is. I think the subtle difference is when
you call gtk_main_quit in foo the object exists and when you call it for
delete the object is allready gone. 

Either way i would have thought it would segfault no matter what. The
correct way to do it would not to try to destroy the widget after
gtk_main as all the widget should have been deleted. Perhaps it is a
complete fluke that it doesnt segfault one way because one function
nulls out the pointer and the other doesn't?

It would be good to get someone with some real knowledge of the gtk
internals to shed some light on this one...


On Thu, 2004-09-09 at 21:51, Alexey Dokuchaev wrote:
Hello there,

I've came across a situation with a code like this (GTK+-2 app):

<pseudo-code>
foo()
{
      window = gtk_window_new();
      button = gtk_button_new_with_label("foo");
      gtk_container_add(window, button);
      gtk_widget_show(button);

      g_signal_connect(window, "delete_event",
              G_CALLBACK (gtk_main_quit), NULL);
      g_signal_connect(button, "clicked",
              G_CALLBACK (gtk_main_quit), NULL);
      return (window);
}

main()
{
      gtk_init();
      window = foo();
      gtk_widget_show(window);
      gtk_main();
      gtk_widget_destroy(window);     // !!!!!
      return (0);
}
</pseudo-code>

Now, as you might have guessed, this simple app creates a window with a
button, and then makes it so the window can be closed by either closing
the window with window manager, or by clicking the button ``foo'', both
ways via gtk_main_quit().

The actual question is that on practice, two ways do not behave
identically.  Clicking the button closes the all nice and clean.  But,
closing the window with window manager, it blows with segfault:

GLib-GObject-WARNING **: invalid uninstantiatable type `(null)' in cast to `GtkWidget'
Segmentation fault

If I remove the line in main() marked with `!!!!', everything magically
start to work correctly (e.g., both ways are closing the window
cleanly).

Can anyone poor some light on this?  Is gtk_widget_destroy() gets called
somewhere deep in gtk_main_quit() ?  If this is true, how come clicking
the button does work (via the very same gtk_main_quit()) ?  Or maybe
there is some other (on "destroy"?) hook I'm missing?  Am I right
thinking that segfault is triggered by gtk_widget_destroy() being called
twice, in the first place?  Finally, what is the correct way of coding
this sort of thing? :-)

I know this differs from ``Hello, world!'' application in GTK+-2.0
tutorial on gtk.org, but that how the real-life code is coded that I
have to work with (it's not mine).

Any help would be greatly appreciated.

Thanks!

./danfe
_______________________________________________
gtk-app-devel-list mailing list
gtk-app-devel-list gnome org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list
-- 
Jared Kells
kared kared net
http://www.kared.net



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