我尝试编写我的第一个 GTK+ 程序。编译很顺利,但 valgrind 说存在内存泄漏。我找不到那些所以有人能说我做错了什么吗?或者是否有可能在没有内存泄漏的情况下编写图形 Linux 程序?
#include <gtk/gtk.h>
int main(int argc, char* argv[])
{
gtk_init(&argc, &argv);
GtkWidget* window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window), "Hello World");
gtk_container_set_border_width(GTK_CONTAINER(window), 60);
GtkWidget* label = gtk_label_new("Hello, world!");
gtk_container_add(GTK_CONTAINER(window), label);
g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
gtk_widget_show_all(window);
gtk_main();
return 0;
}
gcc -Wall gtkhello.c -o gtkhello $(pkg-config --cflags --libs gtk+-2.0)
valgrind -v ./gtkhello
...
==9395== HEAP SUMMARY:
==9395== in use at exit: 538,930 bytes in 6,547 blocks
==9395== total heap usage: 21,434 allocs, 14,887 frees, 2,964,543 bytes allocated
==9395==
==9395== Searching for pointers to 6,547 not-freed blocks
==9395== Checked 949,656 bytes
==9395==
==9395== LEAK SUMMARY:
==9395== definitely lost: 4,480 bytes in 30 blocks
==9395== indirectly lost: 5,160 bytes in 256 blocks
==9395== possibly lost: 180,879 bytes in 1,716 blocks
==9395== still reachable: 348,411 bytes in 4,545 blocks
==9395== suppressed: 0 bytes in 0 blocks
==9395== Rerun with --leak-check=full to see details of leaked memory
==9395==
==9395== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
==9395== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)