使用没有内存泄漏的 gtk 状态图标
Use gtk status icon without memory leak
我正在尝试使用 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 报告:
definitely lost: 2,608 bytes in 7 blocks
indirectly lost: 13,745 bytes in 554 blocks
我应该如何更正代码以在不导致内存泄漏的情况下显示状态图标?
为了我的目的,这个问题的答案是没有办法让 valgrind 报告我的无泄漏程序没有损失。有各种抑制文件可用,包括 one referenced by the people at GNOME. There are a significant number of bugs posted against GTK+ relating to this concern, and at least a few stack overflow threads。我发现 none 我试过的抑制文件对我有用,生成我自己的抑制文件是浪费时间。
我正在尝试使用 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 报告:
definitely lost: 2,608 bytes in 7 blocks
indirectly lost: 13,745 bytes in 554 blocks
我应该如何更正代码以在不导致内存泄漏的情况下显示状态图标?
为了我的目的,这个问题的答案是没有办法让 valgrind 报告我的无泄漏程序没有损失。有各种抑制文件可用,包括 one referenced by the people at GNOME. There are a significant number of bugs posted against GTK+ relating to this concern, and at least a few stack overflow threads。我发现 none 我试过的抑制文件对我有用,生成我自己的抑制文件是浪费时间。