我正在尝试使用gtk status icon,但我不知道如何在不造成内存泄漏的情况下这样做。
#include <gtk/gtk.h>
GtkWidget *window;
GtkStatusIcon *tray_icon;
static void activate (GtkApplication* app, gpointer user_data)
{
window = gtk_application_window_new (app);
gtk_window_set_title (GTK_WINDOW (window), "Window");
gtk_window_set_default_size (GTK_WINDOW (window), 200, 200);
gtk_widget_show_all (window);
return; // <-- When this is commented out, valgrind finds memory leak.
tray_icon = gtk_status_icon_new();
gtk_status_icon_set_visible(tray_icon, TRUE);
gtk_status_icon_set_tooltip_text(tray_icon, "abc");
}
int main (int argc, char **argv)
{
GtkApplication *app;
int status;
app = gtk_application_new ("org.gtk.example", G_APPLICATION_FLAGS_NONE);
g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
status = g_application_run (G_APPLICATION (app), argc, argv);
g_object_unref (app);
// Here I am trying to clean it up somehow.
gtk_status_icon_set_visible(tray_icon, FALSE);
g_object_unref(G_OBJECT(tray_icon));
return status;
}
当未使用状态图标时(请参阅代码中带注释的return语句),valgrind未找到任何明确或间接丢失的块。状态图标valgrind报告:
绝对丢失:7块中的2,608字节
间接丢失:554块中的13,745字节
如何更正代码以显示状态图标而不会导致内存泄漏?
答案 0 :(得分:1)
就我的目的而言,这个问题的答案是没有办法让valgrind报告我的无泄漏计划没有损失。有各种抑制文件可供使用,包括one referenced by the people at GNOME。针对此问题,针对GTK +发布了大量错误,至少有少数stack overflow threads。我发现没有一个我试过的抑制文件对我起作用,而生成我自己的文件是浪费时间。