在简单 GTK 中启用优化时出现分段错误+应用?
可能为时已晚,但我发现至少奇怪的是,当且仅当使用 gcc 优化编译时,以下几行似乎会导致分段错误,甚至是“-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);
这是回溯:
(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 正是上面列出的第一行,似乎“gtk_dialog_new_with_buttons”是罪魁祸首...
版本:
海湾合作委员会:4.4.3
GTK+:2.20.1
顺便说一句,忘记提及在冲突调用之后注释掉某些行可以防止冲突发生。特别是“gtk_container_add(GTK_CONTAINER(hb), cb);”行
我尝试了几乎所有合适的 GtkTypes/GTK_MACROS 组合,没有什么区别。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
长话短说:当手册说NULL时使用NULL,而不是普通的0!
(因为我无法选择评论作为答案,所以我自己写答案,感谢有用的评论......)
GTK+ 文档是这样说的:
但是那天晚上我很懒,输入了只是一个 '0',其中 NULL 是预期的:
...没有注意到 NULL 是一个指针,在我的 64 位系统中是 64 位宽,而 0 是一个 32 位整数...
另外,似乎在编译器无法检测变量参数列表的不一致:使用 -Wall 悄悄编译的代码。
正如 Myforwik 建议的和 Havoc P 进一步澄清,使用“NULL”而不是“0”解决了问题。谢谢你们!
作为记录,我在 32 位模式下进行了测试编译,其中 NULL 也是 32 位,在这种情况下没有段错误。但它仍然是不正确的,因为文档已经足够清楚了,并且 NULL 不是 0,无论 C++ 委员会成员怎么说! ;-)
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:
But I was lazy that night and typed just a '0' where the NULL was expected:
... 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! ;-)