我有.c文件函数来创建status_icon对象:
file.c
void create_status_icon(GtkWidget *app)
{
GdkPixbuf *pixbuf;
GtkIconTheme *icontheme;
icontheme = gtk_icon_theme_get_default();
pixbuf = gtk_icon_theme_load_icon(icontheme, "application", 22, 0, NULL);
g_return_if_fail(pixbuf);
status_icon = G_OBJECT(gtk_status_icon_new_from_pixbuf(pixbuf)); // <-- uninitialized
g_object_unref(pixbuf);
g_signal_connect(G_OBJECT(tray_icon), "activate", G_CALLBACK(tray_activate_cb), (gpointer)app);
g_signal_connect(G_OBJECT(tray_icon), "popup-menu", G_CALLBACK(tray_popup_menu), (gpointer)app);
}
并将它们放入.h文件
file.h
GObject *status_icon;
void create_status_icon(GtkWidget *app);
我想在其他.c文件和main中使用status_icon但是valgrind抱怨条件跳转或移动取决于未初始化的值。
的main.c
GtkWidget *app;
int main(int argc, char* argv[])
gtk_init(&argc, &argv);
app = application_gui();
/* Create an tray icon */
create_status_icon(app) // <-- uninitialized
如何防止这种情况?
由于
更新valgrind输出:
==15658== Conditional jump or move depends on uninitialised value(s)
==15658== at 0x1929FF17: ??? (in /usr/lib/x86_64-linux-gnu/librsvg-2.so.2.36.4)
==15658== by 0x192A0AE7: rsvg_handle_get_pixbuf_sub (in /usr/lib/x86_64-linux-gnu/librsvg-2.so.2.36.4)
==15658== by 0x19074E45: ??? (in /usr/lib/x86_64-linux-gnu/gdk-pixbuf-2.0/2.10.0/loaders/libpixbufloader-svg.so)
==15658== by 0x6252E8C: gdk_pixbuf_loader_close (in /usr/lib/x86_64-linux-gnu/libgdk_pixbuf-2.0.so.0.2800.1)
==15658== by 0x624F064: ??? (in /usr/lib/x86_64-linux-gnu/libgdk_pixbuf-2.0.so.0.2800.1)
==15658== by 0x6250D3C: gdk_pixbuf_new_from_stream_at_scale (in /usr/lib/x86_64-linux-gnu/libgdk_pixbuf-2.0.so.0.2800.1)
==15658== by 0x56D2528: ??? (in /usr/lib/x86_64-linux-gnu/libgtk-3.so.0.800.2)
==15658== by 0x56D5941: gtk_icon_info_load_icon (in /usr/lib/x86_64-linux-gnu/libgtk-3.so.0.800.2)
==15658== by 0x56D61A2: gtk_icon_info_load_symbolic_for_context (in /usr/lib/x86_64-linux-gnu/libgtk-3.so.0.800.2)
==15658== by 0x56D04BF: ??? (in /usr/lib/x86_64-linux-gnu/libgtk-3.so.0.800.2)
==15658== by 0x56D076C: ??? (in /usr/lib/x86_64-linux-gnu/libgtk-3.so.0.800.2)
==15658== by 0x56E1C01: ??? (in /usr/lib/x86_64-linux-gnu/libgtk-3.so.0.800.2)
答案 0 :(得分:1)
在.h文件中声明变量时,必须使用extern
关键字。在没有extern
的.c文件中还必须有一个相应的变量 definition 。
话虽如此,除非你确切知道自己在做什么,否则强烈不鼓励这样的全局变量。