Calling gtk_widget_destroy()



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



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