简体   繁体   English

如何正确管理GTK3中的内存?

[英]How to properly manage memory in GTK3?

I'm trying to set a small project in C using GTK3. 我正在尝试使用GTK3在C中设置一个小项目。 Coding on linux 64 bits, here is a minimal Hello world program I've set up, inspired from the documentation itself. 在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;
}

Simple enough. 很简单。 It works. 有用。 However, when running the program with valgrind's memcheck module, valgrind complains about memory leaks : 但是,当使用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)

Running with --leak-check=full , I see that GTK allocate a bunch of memory itself, and I'm lost of what needs to be done. 使用--leak-check=full运行,我看到GTK本身分配了一堆内存,而我迷失了需要做的事情。 I though that doing g_object_unref on the GtkApplication was enough to recursively free all widgets of all windows associated with that GtkApplication . 尽管我在GtkApplication上执行g_object_unref足以递归释放与该GtkApplication关联的所有窗口的所有小部件。 Am I wrong ? 我错了吗 ?

What should I do to avoid memory leaks ? 我应该怎么做才能避免内存泄漏? Thank you for your help. 谢谢您的帮助。

Your code is correct and is almost identical to those in the documentation demos. 您的代码是正确的,并且几乎与文档演示中的代码相同。

GTK and GLib allocate memory for buffers, their own memory management, etc. What Valgrind has reported is unlikely to be memory leaks as this memory is used during the execution of the application, and not freed on application exit but left to the OS to clear up. GTK和GLib为缓冲区,它们自己的内存管理等分配内存。Valgrind报告的内容不太可能是内存泄漏,因为此内存在应用程序执行期间使用,并且在应用程序退出时不会释放,而是留给OS清除起来。 This appears as a memory leak to Valgrind. 这似乎是对Valgrind的内存泄漏。 A suppression file can be used to help remove the false positives. 禁止文件可用于帮助消除误报。

Glib and GTK have various options here when running your application to help with debugging. 在运行应用程序时,Glib和GTK在此处具有各种选项以帮助调试。 You may want to look at the G_SLICE environment variable. 您可能需要查看G_SLICE环境变量。 You may find enter link description here useful with information on suppression files near the bottom of the page. 您可能会发现此处的输入链接描述非常有用,其中包含页面底部附近的禁止文件信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM