0

我有在 .c 文件中创建 status_icon 对象的函数:

文件.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 文件中

文件.h

GObject *status_icon;

void create_status_icon(GtkWidget *app);

我想在其他 .c 文件和 main 中使用 status_icon 但 valgrind 抱怨条件跳转或移动取决于未初始化的值。

主程序

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)
4

1 回答 1

1

在 .h 文件中声明变量时,必须使用extern关键字。在没有 .c 文件的 .c 文件中还必须有一个对应的变量定义extern

话虽如此,除非您确切知道自己在做什么,否则强烈建议不要使用此类全局变量。

于 2013-07-08T23:55:14.130 回答