我开始学习GTK-3.0
并开始为我工作的程序创建GUI。
该程序编译良好,没有任何警告,但当我用valgrind
运行时,我看到很多内存泄漏,我无法弄清楚我在这里做错了什么。
我将程序缩减为最小代码,这使您可以轻松解释问题。
以下代码编译得很好,但内存泄漏很多:
#include <gtk/gtk.h>
int main(int argc, char *argv[]){
gtk_init(&argc, &argv);
GtkWidget *window;
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
g_signal_connect(window, "delete-event", G_CALLBACK(gtk_main_quit), NULL);
gtk_widget_show(window);
gtk_main();
}
我用以下代码编译它:
gcc -Wpedantic -std=c11 -Wall -Wextra -Werror -Wstrict-prototypes -Wmissing-prototypes -Wold-style-definition -Wconversion -Wshadow -Winit-self -Wfloat-equal -Wwrite-strings -O0 -g main.c -o app `pkg-config --cflags gtk+-3.0 ` `pkg-config --libs gtk+-3.0 `
并在使用以下命令运行valgrind
之后:
valgrind --leak-check=full --track-origins=yes --leak-check=full ./app
我得到以下输出:
==14405== Memcheck, a memory error detector
==14405== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==14405== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==14405== Command: ./app
==14405==
==14405==
==14405== HEAP SUMMARY:
==14405== in use at exit: 1,243,902 bytes in 12,774 blocks
==14405== total heap usage: 80,535 allocs, 67,761 frees, 6,190,649 bytes allocated
==14405==
==14405== 16 bytes in 1 blocks are possibly lost in loss record 1,279 of 5,945
==14405== at 0x4C2FB55: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==14405== by 0x86E2780: g_malloc0 (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.4800.1)
==14405== by 0x5796522: ??? (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.1)
==14405== by 0x579AF90: g_type_register_fundamental (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.1)
==14405== by 0x577A30B: ??? (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.1)
==14405== by 0x5774177: ??? (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.1)
==14405== by 0x40104E9: call_init.part.0 (dl-init.c:72)
==14405== by 0x40105FA: call_init (dl-init.c:30)
==14405== by 0x40105FA: _dl_init (dl-init.c:120)
==14405== by 0x4000CF9: ??? (in /lib/x86_64-linux-gnu/ld-2.23.so)
==14405==
==14405== 16 bytes in 1 blocks are possibly lost in loss record 1,280 of 5,945
==14405== at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==14405== by 0x4C2FDEF: realloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==14405== by 0x86E27E7: g_realloc (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.4800.1)
==14405== by 0x5796406: ??? (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.1)
==14405== by 0x579AF90: g_type_register_fundamental (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.1)
==14405== by 0x577A30B: ??? (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.1)
==14405== by 0x5774177: ??? (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.1)
==14405== by 0x40104E9: call_init.part.0 (dl-init.c:72)
==14405== by 0x40105FA: call_init (dl-init.c:30)
==14405== by 0x40105FA: _dl_init (dl-init.c:120)
==14405== by 0x4000CF9: ??? (in /lib/x86_64-linux-gnu/ld-2.23.so)
==14405==
==14405== 16 bytes in 1 blocks are possibly lost in loss record 1,281 of 5,945
==14405== at 0x4C2FB55: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==14405== by 0x86E2780: g_malloc0 (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.4800.1)
==14405== by 0x5796522: ??? (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.1)
==14405== by 0x579AF90: g_type_register_fundamental (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.1)
==14405== by 0x577A371: ??? (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.1)
==14405== by 0x5774177: ??? (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.1)
==14405== by 0x40104E9: call_init.part.0 (dl-init.c:72)
==14405== by 0x40105FA: call_init (dl-init.c:30)
==14405== by 0x40105FA: _dl_init (dl-init.c:120)
==14405== by 0x4000CF9: ??? (in /lib/x86_64-linux-gnu/ld-2.23.so)
==14405==
==14405== 16 bytes in 1 blocks are possibly lost in loss record 1,282 of 5,945
==14405== at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==14405== by 0x4C2FDEF: realloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==14405== by 0x86E27E7: g_realloc (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.4800.1)
==14405== by 0x5796406: ??? (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.1)
==14405== by 0x579AF90: g_type_register_fundamental (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.1)
==14405== by 0x577A371: ??? (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.1)
==14405== by 0x5774177: ??? (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.1)
==14405== by 0x40104E9: call_init.part.0 (dl-init.c:72)
==14405== by 0x40105FA: call_init (dl-init.c:30)
==14405== by 0x40105FA: _dl_init (dl-init.c:120)
==14405== by 0x4000CF9: ??? (in /lib/x86_64-linux-gnu/ld-2.23.so)
==14405==
==14405== 16 bytes in 1 blocks are possibly lost in loss record 1,283 of 5,945
==14405== at 0x4C2FB55: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==14405== by 0x86E2780: g_malloc0 (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.4800.1)
==14405== by 0x5796522: ??? (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.1)
==14405== by 0x579AF90: g_type_register_fundamental (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.1)
==14405== by 0x5783B04: ??? (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.1)
==14405== by 0x5774181: ??? (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.1)
==14405== by 0x40104E9: call_init.part.0 (dl-init.c:72)
==14405== by 0x40105FA: call_init (dl-init.c:30)
==14405== by 0x40105FA: _dl_init (dl-init.c:120)
==14405== by 0x4000CF9: ??? (in /lib/x86_64-linux-gnu/ld-2.23.so)
==14405==
==14405== 16 bytes in 1 blocks are possibly lost in loss record 1,284 of 5,945
==14405== at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==14405== by 0x4C2FDEF: realloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==14405== by 0x86E27E7: g_realloc (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.4800.1)
==14405== by 0x5796406: ??? (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.1)
==14405== by 0x579AF90: g_type_register_fundamental (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.1)
==14405== by 0x5783B04: ??? (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.1)
==14405== by 0x5774181: ??? (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.1)
==14405== by 0x40104E9: call_init.part.0 (dl-init.c:72)
==14405== by 0x40105FA: call_init (dl-init.c:30)
==14405== by 0x40105FA: _dl_init (dl-init.c:120)
==14405== by 0x4000CF9: ??? (in /lib/x86_64-linux-gnu/ld-2.23.so)
==14405==
==14405== 16 bytes in 1 blocks are possibly lost in loss record 1,285 of 5,945
==14405== at 0x4C2FB55: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==14405== by 0x86E2780: g_malloc0 (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.4800.1)
==14405== by 0x5796522: ??? (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.1)
==14405== by 0x579AF90: g_type_register_fundamental (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.1)
==14405== by 0x577EC1B: ??? (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.1)
==14405== by 0x5774186: ??? (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.1)
==14405== by 0x40104E9: call_init.part.0 (dl-init.c:72)
==14405== by 0x40105FA: call_init (dl-init.c:30)
==14405== by 0x40105FA: _dl_init (dl-init.c:120)
==14405== by 0x4000CF9: ??? (in /lib/x86_64-linux-gnu/ld-2.23.so)
==14405==
==14405== 16 bytes in 1 blocks are possibly lost in loss record 1,286 of 5,945
==14405== at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==14405== by 0x4C2FDEF: realloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==14405== by 0x86E27E7: g_realloc (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.4800.1)
==14405== by 0x5796406: ??? (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.1)
==14405== by 0x579AF90: g_type_register_fundamental (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.1)
==14405== by 0x577EC1B: ??? (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.1)
==14405== by 0x5774186: ??? (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.1)
==14405== by 0x40104E9: call_init.part.0 (dl-init.c:72)
==14405== by 0x40105FA: call_init (dl-init.c:30)
==14405== by 0x40105FA: _dl_init (dl-init.c:120)
==14405== by 0x4000CF9: ??? (in /lib/x86_64-linux-gnu/ld-2.23.so)
==14405==
==14405== 24 bytes in 1 blocks are possibly lost in loss record 1,808 of 5,945
==14405== at 0x4C2FB55: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==14405== by 0x86E2780: g_malloc0 (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.4800.1)
==14405== by 0x57993B5: g_type_class_ref (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.1)
==14405== by 0x5798E2C: g_type_class_ref (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.1)
==14405== by 0x5788147: g_param_spec_flags (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.1)
==14405== by 0x5191F6F: ??? (in /usr/lib/x86_64-linux-gnu/libgtk-3.so.0.1800.9)
==14405== by 0x579922C: g_type_class_ref (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.1)
==14405== by 0x4EBFC49: ??? (in /usr/lib/x86_64-linux-gnu/libgtk-3.so.0.1800.9)
==14405== by 0x7532A1D: atk_add_focus_tracker (in /usr/lib/x86_64-linux-gnu/libatk-1.0.so.0.21809.1)
==14405== by 0x4EC0084: ??? (in /usr/lib/x86_64-linux-gnu/libgtk-3.so.0.1800.9)
==14405== by 0x5778FA4: g_closure_invoke (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.1)
==14405== by 0x578AFC0: ??? (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.1)
==14405==
==14405== 32 bytes in 1 blocks are possibly lost in loss record 2,824 of 5,945
==14405== at 0x4C2FB55: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==14405== by 0x86E2780: g_malloc0 (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.4800.1)
==14405== by 0x57993B5: g_type_class_ref (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.1)
==14405== by 0x5798E2C: g_type_class_ref (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.1)
==14405== by 0x5788057: g_param_spec_enum (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.1)
==14405== by 0x5FCBAFE: ??? (in /usr/lib/x86_64-linux-gnu/libgdk-3.so.0.1800.9)
==14405== by 0x579922C: g_type_class_ref (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.1)
==14405== by 0x5798E2C: g_type_class_ref (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.1)
==14405== by 0x57803A3: g_object_new_valist (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.1)
==14405== by 0x5780520: g_object_new (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.1)
==14405== by 0x5FF16DD: ??? (in /usr/lib/x86_64-linux-gnu/libgdk-3.so.0.1800.9)
==14405== by 0x5FF3E21: ??? (in /usr/lib/x86_64-linux-gnu/libgdk-3.so.0.1800.9)
==14405==
==14405== 56 bytes in 1 blocks are possibly lost in loss record 3,553 of 5,945
==14405== at 0x4C2FD5F: realloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==14405== by 0x86E27E7: g_realloc (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.4800.1)
==14405== by 0x57959D8: ??? (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.1)
==14405== by 0x579B2CC: g_type_register_static (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.1)
==14405== by 0x577A53D: g_flags_register_static (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.1)
==14405== by 0x50A2D0A: ??? (in /usr/lib/x86_64-linux-gnu/libgtk-3.so.0.1800.9)
==14405== by 0x4FB9264: ??? (in /usr/lib/x86_64-linux-gnu/libgtk-3.so.0.1800.9)
==14405== by 0x579922C: g_type_class_ref (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.1)
==14405== by 0x57803A3: g_object_new_valist (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.1)
==14405== by 0x5780520: g_object_new (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.1)
==14405== by 0x4FBAAB7: ??? (in /usr/lib/x86_64-linux-gnu/libgtk-3.so.0.1800.9)
==14405== by 0x4FBAE82: ??? (in /usr/lib/x86_64-linux-gnu/libgtk-3.so.0.1800.9)
==14405==
==14405== 80 bytes in 1 blocks are possibly lost in loss record 4,221 of 5,945
==14405== at 0x4C2FB55: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==14405== by 0x86E2780: g_malloc0 (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.4800.1)
==14405== by 0x57993B5: g_type_class_ref (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.1)
==14405== by 0x5798E2C: g_type_class_ref (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.1)
==14405== by 0x579C247: g_type_create_instance (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.1)
==14405== by 0x57840D2: g_param_spec_internal (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.1)
==14405== by 0x5788661: g_param_spec_object (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.1)
==14405== by 0x5FD1231: ??? (in /usr/lib/x86_64-linux-gnu/libgdk-3.so.0.1800.9)
==14405== by 0x579922C: g_type_class_ref (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.1)
==14405== by 0x577FDAC: g_object_newv (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.1)
==14405== by 0x5780533: g_object_new (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.1)
==14405== by 0x5FD1324: gdk_display_manager_get (in /usr/lib/x86_64-linux-gnu/libgdk-3.so.0.1800.9)
==14405==
==14405== 96 bytes in 1 blocks are possibly lost in loss record 5,132 of 5,945
==14405== at 0x4C2FB55: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==14405== by 0x86E2780: g_malloc0 (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.4800.1)
==14405== by 0x5795A59: ??? (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.1)
==14405== by 0x5795B43: ??? (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.1)
==14405== by 0x57740AA: ??? (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.1)
==14405== by 0x40104E9: call_init.part.0 (dl-init.c:72)
==14405== by 0x40105FA: call_init (dl-init.c:30)
==14405== by 0x40105FA: _dl_init (dl-init.c:120)
==14405== by 0x4000CF9: ??? (in /lib/x86_64-linux-gnu/ld-2.23.so)
==14405==
==14405== 96 bytes in 1 blocks are possibly lost in loss record 5,133 of 5,945
==14405== at 0x4C2FB55: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==14405== by 0x86E2780: g_malloc0 (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.4800.1)
==14405== by 0x5795A59: ??? (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.1)
==14405== by 0x5795B43: ??? (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.1)
==14405== by 0x579AF82: g_type_register_fundamental (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.1)
==14405== by 0x577A30B: ??? (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.1)
==14405== by 0x5774177: ??? (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.1)
==14405== by 0x40104E9: call_init.part.0 (dl-init.c:72)
==14405== by 0x40105FA: call_init (dl-init.c:30)
==14405== by 0x40105FA: _dl_init (dl-init.c:120)
==14405== by 0x4000CF9: ??? (in /lib/x86_64-linux-gnu/ld-2.23.so)
==14405==
==14405== 96 bytes in 1 blocks are possibly lost in loss record 5,134 of 5,945
==14405== at 0x4C2FB55: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==14405== by 0x86E2780: g_malloc0 (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.4800.1)
==14405== by 0x5795A59: ??? (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.1)
==14405== by 0x5795B43: ??? (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.1)
==14405== by 0x579AF82: g_type_register_fundamental (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.1)
==14405== by 0x577A371: ??? (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.1)
==14405== by 0x5774177: ??? (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.1)
==14405== by 0x40104E9: call_init.part.0 (dl-init.c:72)
==14405== by 0x40105FA: call_init (dl-init.c:30)
==14405== by 0x40105FA: _dl_init (dl-init.c:120)
==14405== by 0x4000CF9: ??? (in /lib/x86_64-linux-gnu/ld-2.23.so)
==14405==
==14405== 96 bytes in 1 blocks are possibly lost in loss record 5,135 of 5,945
==14405== at 0x4C2FB55: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==14405== by 0x86E2780: g_malloc0 (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.4800.1)
==14405== by 0x5795A59: ??? (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.1)
==14405== by 0x5795B43: ??? (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.1)
==14405== by 0x579AF82: g_type_register_fundamental (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.1)
==14405== by 0x5783B04: ??? (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.1)
==14405== by 0x5774181: ??? (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.1)
==14405== by 0x40104E9: call_init.part.0 (dl-init.c:72)
==14405== by 0x40105FA: call_init (dl-init.c:30)
==14405== by 0x40105FA: _dl_init (dl-init.c:120)
==14405== by 0x4000CF9: ??? (in /lib/x86_64-linux-gnu/ld-2.23.so)
==14405==
==14405== 96 bytes in 1 blocks are possibly lost in loss record 5,136 of 5,945
==14405== at 0x4C2FB55: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==14405== by 0x86E2780: g_malloc0 (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.4800.1)
==14405== by 0x5795A59: ??? (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.1)
==14405== by 0x5795B43: ??? (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.1)
==14405== by 0x579AF82: g_type_register_fundamental (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.1)
==14405== by 0x577EC1B: ??? (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.1)
==14405== by 0x5774186: ??? (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.1)
==14405== by 0x40104E9: call_init.part.0 (dl-init.c:72)
==14405== by 0x40105FA: call_init (dl-init.c:30)
==14405== by 0x40105FA: _dl_init (dl-init.c:120)
==14405== by 0x4000CF9: ??? (in /lib/x86_64-linux-gnu/ld-2.23.so)
==14405==
==14405== 132 bytes in 1 blocks are possibly lost in loss record 5,413 of 5,945
==14405== at 0x4C2FB55: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==14405== by 0x86E2780: g_malloc0 (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.4800.1)
==14405== by 0x5796DFF: ??? (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.1)
==14405== by 0x579AFE0: g_type_register_fundamental (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.1)
==14405== by 0x577A30B: ??? (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.1)
==14405== by 0x5774177: ??? (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.1)
==14405== by 0x40104E9: call_init.part.0 (dl-init.c:72)
==14405== by 0x40105FA: call_init (dl-init.c:30)
==14405== by 0x40105FA: _dl_init (dl-init.c:120)
==14405== by 0x4000CF9: ??? (in /lib/x86_64-linux-gnu/ld-2.23.so)
==14405==
==14405== 132 bytes in 1 blocks are possibly lost in loss record 5,414 of 5,945
==14405== at 0x4C2FB55: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==14405== by 0x86E2780: g_malloc0 (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.4800.1)
==14405== by 0x5796DFF: ??? (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.1)
==14405== by 0x579AFE0: g_type_register_fundamental (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.1)
==14405== by 0x577A371: ??? (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.1)
==14405== by 0x5774177: ??? (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.1)
==14405== by 0x40104E9: call_init.part.0 (dl-init.c:72)
==14405== by 0x40105FA: call_init (dl-init.c:30)
==14405== by 0x40105FA: _dl_init (dl-init.c:120)
==14405== by 0x4000CF9: ??? (in /lib/x86_64-linux-gnu/ld-2.23.so)
==14405==
==14405== 148 bytes in 1 blocks are possibly lost in loss record 5,457 of 5,945
==14405== at 0x4C2FB55: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==14405== by 0x86E2780: g_malloc0 (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.4800.1)
==14405== by 0x5796BD6: ??? (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.1)
==14405== by 0x579AFE0: g_type_register_fundamental (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.1)
==14405== by 0x5783B04: ??? (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.1)
==14405== by 0x5774181: ??? (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.1)
==14405== by 0x40104E9: call_init.part.0 (dl-init.c:72)
==14405== by 0x40105FA: call_init (dl-init.c:30)
==14405== by 0x40105FA: _dl_init (dl-init.c:120)
==14405== by 0x4000CF9: ??? (in /lib/x86_64-linux-gnu/ld-2.23.so)
==14405==
==14405== 148 bytes in 1 blocks are possibly lost in loss record 5,458 of 5,945
==14405== at 0x4C2FB55: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==14405== by 0x86E2780: g_malloc0 (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.4800.1)
==14405== by 0x5796BD6: ??? (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.1)
==14405== by 0x579AFE0: g_type_register_fundamental (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.1)
==14405== by 0x577EC1B: ??? (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.1)
==14405== by 0x5774186: ??? (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.1)
==14405== by 0x40104E9: call_init.part.0 (dl-init.c:72)
==14405== by 0x40105FA: call_init (dl-init.c:30)
==14405== by 0x40105FA: _dl_init (dl-init.c:120)
==14405== by 0x4000CF9: ??? (in /lib/x86_64-linux-gnu/ld-2.23.so)
==14405==
==14405== 176 bytes in 4 blocks are possibly lost in loss record 5,513 of 5,945
==14405== at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==14405== by 0x86E2728: g_malloc (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.4800.1)
==14405== by 0x4FA7173: ??? (in /usr/lib/x86_64-linux-gnu/libgtk-3.so.0.1800.9)
==14405== by 0x4FA7439: ??? (in /usr/lib/x86_64-linux-gnu/libgtk-3.so.0.1800.9)
==14405== by 0x4FA622C: ??? (in /usr/lib/x86_64-linux-gnu/libgtk-3.so.0.1800.9)
==14405== by 0x4F02766: ??? (in /usr/lib/x86_64-linux-gnu/libgtk-3.so.0.1800.9)
==14405== by 0x4F03FE4: ??? (in /usr/lib/x86_64-linux-gnu/libgtk-3.so.0.1800.9)
==14405== by 0x4F0425D: ??? (in /usr/lib/x86_64-linux-gnu/libgtk-3.so.0.1800.9)
==14405== by 0x577E896: ??? (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.1)
==14405== by 0x57801B4: g_object_new_valist (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.1)
==14405== by 0x5780520: g_object_new (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.1)
==14405== by 0x4F02D27: ??? (in /usr/lib/x86_64-linux-gnu/libgtk-3.so.0.1800.9)
==14405==
==14405== 184 bytes in 1 blocks are possibly lost in loss record 5,528 of 5,945
==14405== at 0x4C2FD5F: realloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==14405== by 0x86E27E7: g_realloc (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.4800.1)
==14405== by 0x57959D8: ??? (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.1)
==14405== by 0x579B2CC: g_type_register_static (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.1)
==14405== by 0x5785799: g_param_type_register_static (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.1)
==14405== by 0x5787AFB: ??? (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.1)
==14405== by 0x577418B: ??? (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.1)
==14405== by 0x40104E9: call_init.part.0 (dl-init.c:72)
==14405== by 0x40105FA: call_init (dl-init.c:30)
==14405== by 0x40105FA: _dl_init (dl-init.c:120)
==14405== by 0x4000CF9: ??? (in /lib/x86_64-linux-gnu/ld-2.23.so)
==14405==
==14405== 200 bytes in 5 blocks are possibly lost in loss record 5,549 of 5,945
==14405== at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==14405== by 0x86E2728: g_malloc (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.4800.1)
==14405== by 0x86FB537: g_memdup (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.4800.1)
==14405== by 0x4FA7109: ??? (in /usr/lib/x86_64-linux-gnu/libgtk-3.so.0.1800.9)
==14405== by 0x4FA7387: ??? (in /usr/lib/x86_64-linux-gnu/libgtk-3.so.0.1800.9)
==14405== by 0x4FA61EC: ??? (in /usr/lib/x86_64-linux-gnu/libgtk-3.so.0.1800.9)
==14405== by 0x50EFD11: gtk_style_context_set_state (in /usr/lib/x86_64-linux-gnu/libgtk-3.so.0.1800.9)
==14405== by 0x4F03EE4: ??? (in /usr/lib/x86_64-linux-gnu/libgtk-3.so.0.1800.9)
==14405== by 0x4F0425D: ??? (in /usr/lib/x86_64-linux-gnu/libgtk-3.so.0.1800.9)
==14405== by 0x577E896: ??? (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.1)
==14405== by 0x57801B4: g_object_new_valist (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.1)
==14405== by 0x5780520: g_object_new (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.1)
==14405==
==14405== 336 bytes in 1 blocks are possibly lost in loss record 5,673 of 5,945
==14405== at 0x4C2FB55: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==14405== by 0x40136D4: allocate_dtv (dl-tls.c:322)
==14405== by 0x40136D4: _dl_allocate_tls (dl-tls.c:539)
==14405== by 0x59C42AE: allocate_stack (allocatestack.c:588)
==14405== by 0x59C42AE: pthread_create@@GLIBC_2.2.5 (pthread_create.c:539)
==14405== by 0x87218AF: ??? (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.4800.1)
==14405== by 0x8703E9E: ??? (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.4800.1)
==14405== by 0x8703F57: g_thread_new (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.4800.1)
==14405== by 0x86DE280: ??? (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.4800.1)
==14405== by 0x8392E16: g_task_get_type (in /usr/lib/x86_64-linux-gnu/libgio-2.0.so.0.4800.1)
==14405== by 0x83F0610: ??? (in /usr/lib/x86_64-linux-gnu/libgio-2.0.so.0.4800.1)
==14405== by 0x83E4216: ??? (in /usr/lib/x86_64-linux-gnu/libgio-2.0.so.0.4800.1)
==14405==
==14405==
==14405== LEAK SUMMARY:
==14405== definitely lost: 0 bytes in 0 blocks
==14405== indirectly lost: 0 bytes in 0 blocks
==14405== possibly lost: 3,544 bytes in 35 blocks
==14405== still reachable: 1,160,006 bytes in 12,091 blocks
==14405== of which reachable via heuristic:
==14405== length64 : 4,080 bytes in 72 blocks
==14405== newarray : 2,112 bytes in 52 blocks
==14405== suppressed: 0 bytes in 0 blocks
==14405== Reachable blocks (those to which a pointer was found) are not shown.
==14405== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==14405==
==14405== For counts of detected and suppressed errors, rerun with: -v
==14405== ERROR SUMMARY: 28 errors from 28 contexts (suppressed: 0 from 0)
在我读完一本书并跟着GTK3 Manual后,我就可以知道泄漏在哪里。
我在这里开始使用GUI了吗?从手册中我几乎可以肯定我是以正确的方式编码的。
我在valgrind
输出中看到calloc
和realloc
被调用,这意味着它也应该是free()
调用。
如果我必须这样做,如何使用GTK +释放内存?
答案 0 :(得分:2)
这已经多次回答了。这就是我给出的一个答案给那些在我曾经温和的论坛上问这个问题的人:
GTK +在分配和释放应用程序生命周期所需的内部缓冲区方面非常懒惰。例如,它可以在初始化期间为查找表分配存储器区域,这是应用程序生命周期所需的。 GTK +将永远不会解除分配。对于Valgrind来说,这看起来像是内存泄漏(从技术上来说),但是作为优化,GTK +不会释放它,因为它将在应用程序退出期间解除分配,因此不会出错。这就是您需要抑制文件的原因,以便Valgrind可以忽略这些文件。问题是您需要在大多数GTK +版本更改时更改这些。
你所看到的是误报。您可以使用抑制文件,以便valgrind可以忽略这些。您可以创建自己的或搜索已经完成的。