Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Segmentation fault when enabling optimization in a simple GTK+ application?

Might be that it is too late, but I find it at least curious that the following few lines seem to be causing a segmentation fault if and only when compiled with gcc's optimization, even "-O1"!

settings_dialog = gtk_dialog_new_with_buttons("gatotray Settings"
    , NULL, 0, GTK_STOCK_CANCEL, FALSE, GTK_STOCK_SAVE, TRUE, 0);
g_signal_connect(G_OBJECT(settings_dialog), "response", G_CALLBACK(gtk_widget_destroy), NULL);
g_signal_connect(G_OBJECT(settings_dialog), "destroy", G_CALLBACK(settings_destroyed), NULL);
GtkWidget *vb = gtk_dialog_get_content_area(GTK_DIALOG(settings_dialog));
GtkWidget *hb = gtk_hbox_new(FALSE, 3);
gtk_container_add(GTK_CONTAINER(hb), gtk_label_new("Background:"));
GtkWidget *cb = gtk_color_button_new();
gtk_container_add(GTK_CONTAINER(hb), cb);
gtk_container_add(GTK_CONTAINER(vb), hb);

This is the backtrace:

(gdb) backtrace 
#0  0x00007ffff4d88052 in ?? () from /lib/libc.so.6
#1  0x00007ffff5304112 in g_strdup () from /lib/libglib-2.0.so.0
#2  0x00007ffff5bc799d in ?? () from /usr/lib/libgobject-2.0.so.0
#3  0x00007ffff5ba826c in g_object_new_valist ()
   from /usr/lib/libgobject-2.0.so.0
#4  0x00007ffff5ba84f1 in g_object_new () from /usr/lib/libgobject-2.0.so.0
#5  0x00007ffff78502d5 in gtk_button_new_from_stock ()
   from /usr/lib/libgtk-x11-2.0.so.0
#6  0x00007ffff787cc95 in gtk_dialog_add_button ()
   from /usr/lib/libgtk-x11-2.0.so.0
#7  0x00007ffff787cd60 in ?? () from /usr/lib/libgtk-x11-2.0.so.0
#8  0x00007ffff787cf60 in gtk_dialog_new_with_buttons ()
   from /usr/lib/libgtk-x11-2.0.so.0
#9  0x0000000000402bb9 in show_settings_dialog () at settings.c:24
#10 0x0000000000403328 in main (argc=1, argv=0x7fffffffe2b8) at gatotray.c:286

... settings.c:24 is exactly the first line listed above, seems like "gtk_dialog_new_with_buttons" is the culprit...

Versions:
gcc: 4.4.3
GTK+: 2.20.1

BTW, forgot to mention that commenting out certain lines after the conflictive call prevents it from happening. Particularly the line with "gtk_container_add(GTK_CONTAINER(hb), cb);"

I tried almost all suitable combinations of GtkTypes/GTK_MACROS, it makes no difference.

like image 597
gatopeich Avatar asked Oct 25 '22 21:10

gatopeich


1 Answers

Long story short: use NULL when the manual says NULL, and not a plain 0!

(Since I can't choose the comments as an answer, I am writing the answer myself, giving credit to the helpful comments...)

GTK+ documentation states this:

GtkWidget*
gtk_dialog_new_with_buttons (const gchar    *title,
                             GtkWindow      *parent,
                             GtkDialogFlags  flags,
                             const gchar    *first_button_text,
                         ...);

title : Title of the dialog, or NULL. allow-none.
parent : Transient parent of the dialog, or NULL. allow-none.
flags : from GtkDialogFlags
first_button_text : stock ID or text to go in first button, or NULL. allow-none.
... : response ID for first button, then additional buttons, ending with NULL

But I was lazy that night and typed just a '0' where the NULL was expected:

settings_dialog = GTK_DIALOG(gtk_dialog_new_with_buttons("gatotray Settings"
    , NULL, 0, GTK_STOCK_CANCEL, FALSE, GTK_STOCK_SAVE, TRUE, 0));

... Not noticing that NULL is a pointer which in my 64-bits system is 64-bits wide, whereas 0 is a 32-bit integer...

Also, seems like in the variable argument list the compiler was not able to detect the inconsistency: the code compiled quietly with -Wall.

As Myforwik suggested and Havoc P further clarified, using 'NULL' there instead of the '0' fixed the issue. Thanks guys!

For the record, I did a test compiling in 32 bits mode where NULL is also 32-bits, and in that case there was no segfault. It is still incorrect though, since the documentation is clear enough, and NULL is not 0, no matter what the C++ committeemen ever say! ;-)

like image 104
gatopeich Avatar answered Oct 31 '22 09:10

gatopeich