[英]How to properly manage memory in GTK3?
我正在嘗試使用GTK3在C中設置一個小項目。 在Linux 64位上進行編碼,這是我從文檔本身得到啟發而建立的最小的Hello world程序。
#include <gtk/gtk.h>
static void
activate (GtkApplication* app,
gpointer user_data)
{
GtkWidget *window;
GtkWidget *label;
window = gtk_application_window_new( app );
gtk_window_set_title( GTK_WINDOW( window ), "Minimal GTK" );
gtk_window_set_default_size( GTK_WINDOW( window ), 400, 300 );
label = gtk_label_new( "Some men just want to watch the world burn." );
gtk_container_add( GTK_CONTAINER(window), label );
gtk_widget_show_all( window );
}
int
main(int argc,
char **argv)
{
GtkApplication *app;
int app_status;
app = gtk_application_new( "com.github.laerne.minimal_gtk", G_APPLICATION_FLAGS_NONE );
g_signal_connect( app, "activate", G_CALLBACK(activate), NULL );
app_status = g_application_run( G_APPLICATION(app), argc, argv );
g_object_unref( app );
return app_status;
}
很簡單。 有用。 但是,當使用valgrind的memcheck模塊運行程序時,valgrind抱怨內存泄漏:
==11415== LEAK SUMMARY:
==11415== definitely lost: 1,856 bytes in 4 blocks
==11415== indirectly lost: 7,455 bytes in 320 blocks
==11415== possibly lost: 4,899 bytes in 56 blocks
==11415== still reachable: 1,809,562 bytes in 22,030 blocks
==11415== of which reachable via heuristic:
==11415== length64 : 6,240 bytes in 102 blocks
==11415== newarray : 2,144 bytes in 54 blocks
==11415== suppressed: 0 bytes in 0 blocks
==11415== Reachable blocks (those to which a pointer was found) are not shown.
==11415== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==11415==
==11415== For counts of detected and suppressed errors, rerun with: -v
==11415== ERROR SUMMARY: 42 errors from 42 contexts (suppressed: 0 from 0)
使用--leak-check=full
運行,我看到GTK本身分配了一堆內存,而我迷失了需要做的事情。 盡管我在GtkApplication
上執行g_object_unref
足以遞歸釋放與該GtkApplication
關聯的所有窗口的所有小部件。 我錯了嗎 ?
我應該怎么做才能避免內存泄漏? 謝謝您的幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.