当前位置:  开发笔记 > 编程语言 > 正文

C++中"仍然可以访问"和"可能丢失"的块的valgrind输出不会引用我的源代码

如何解决《C++中"仍然可以访问"和"可能丢失"的块的valgrind输出不会引用我的源代码》经验,为你挑选了1个好方法。

我很难确定我的代码中的内存泄漏位置.

我运行的valgrind命令:

valgrind --leak-check=full --log-file=vg1.log --show-leak-kinds=all --leak-resolution=low --track-origins=yes --leak-check-heuristics=all ./enalu_dbg

和输出

==22866== Memcheck, a memory error detector
==22866== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==22866== Using Valgrind-3.10.1 and LibVEX; rerun with -h for copyright info
==22866== Command: ./enalu_dbg
==22866== Parent PID: 21933
==22866== 
==22866== 
==22866== HEAP SUMMARY:
==22866==     in use at exit: 47,252 bytes in 240 blocks
==22866==   total heap usage: 288 allocs, 48 frees, 55,138 bytes allocated
==22866== 
==22866== 4 bytes in 1 blocks are still reachable in loss record 1 of 23
==22866==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==22866==    by 0x77018CD: ??? (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.4002.0)
==22866==    by 0x7701D28: g_private_get (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.4002.0)
==22866==    by 0x76DB20C: g_slice_alloc (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.4002.0)
==22866==    by 0x76AF17D: g_hash_table_new_full (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.4002.0)
==22866==    by 0x76CF494: g_quark_from_static_string (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.4002.0)
==22866==    by 0x74314AB: ??? (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4002.0)
==22866==    by 0x4010139: call_init.part.0 (dl-init.c:78)
==22866==    by 0x4010222: call_init (dl-init.c:36)
==22866==    by 0x4010222: _dl_init (dl-init.c:126)
==22866==    by 0x4001309: ??? (in /lib/x86_64-linux-gnu/ld-2.19.so)

...

==22866== 184 bytes in 1 blocks are possibly lost in loss record 13 of 23
==22866==    at 0x4C2CE8E: realloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==22866==    by 0x76C56AE: g_realloc (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.4002.0)
==22866==    by 0x7451618: ??? (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4002.0)
==22866==    by 0x74560D4: g_type_register_static (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4002.0)
==22866==    by 0x7442DE6: g_param_type_register_static (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4002.0)
==22866==    by 0x74449AB: ??? (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4002.0)
==22866==    by 0x74315E9: ??? (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4002.0)
==22866==    by 0x4010139: call_init.part.0 (dl-init.c:78)
==22866==    by 0x4010222: call_init (dl-init.c:36)
==22866==    by 0x4010222: _dl_init (dl-init.c:126)
==22866==    by 0x4001309: ??? (in /lib/x86_64-linux-gnu/ld-2.19.so)
==22866== 

...

==22866== 6,028 bytes in 60 blocks are still reachable in loss record 21 of 23
==22866==    at 0x4C2CC70: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==22866==    by 0x76C5668: g_malloc0 (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.4002.0)
==22866==    by 0x74514D9: ??? (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4002.0)
==22866==    by 0x74560D4: g_type_register_static (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4002.0)
==22866==    by 0x7442DE6: g_param_type_register_static (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4002.0)
==22866==    by 0x744423A: ??? (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4002.0)
==22866==    by 0x74315E9: ??? (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4002.0)
==22866==    by 0x4010139: call_init.part.0 (dl-init.c:78)
==22866==    by 0x4010222: call_init (dl-init.c:36)
==22866==    by 0x4010222: _dl_init (dl-init.c:126)
==22866==    by 0x4001309: ??? (in /lib/x86_64-linux-gnu/ld-2.19.so)
==22866== 
==22866== 10,360 bytes in 5 blocks are still reachable in loss record 22 of 23
==22866==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==22866==    by 0x8B16E9A: ??? (in /usr/lib/x86_64-linux-gnu/libpixman-1.so.0.30.2)
==22866==    by 0x8B15ACE: ??? (in /usr/lib/x86_64-linux-gnu/libpixman-1.so.0.30.2)
==22866==    by 0x8B17585: ??? (in /usr/lib/x86_64-linux-gnu/libpixman-1.so.0.30.2)
==22866==    by 0x8AC9508: ??? (in /usr/lib/x86_64-linux-gnu/libpixman-1.so.0.30.2)
==22866==    by 0x4010139: call_init.part.0 (dl-init.c:78)
==22866==    by 0x4010222: call_init (dl-init.c:36)
==22866==    by 0x4010222: _dl_init (dl-init.c:126)
==22866==    by 0x4001309: ??? (in /lib/x86_64-linux-gnu/ld-2.19.so)
==22866== 
==22866== 16,600 bytes in 4 blocks are still reachable in loss record 23 of 23
==22866==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==22866==    by 0x76C5610: g_malloc (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.4002.0)
==22866==    by 0x76CF445: g_quark_from_static_string (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.4002.0)
==22866==    by 0x74314AB: ??? (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4002.0)
==22866==    by 0x4010139: call_init.part.0 (dl-init.c:78)
==22866==    by 0x4010222: call_init (dl-init.c:36)
==22866==    by 0x4010222: _dl_init (dl-init.c:126)
==22866==    by 0x4001309: ??? (in /lib/x86_64-linux-gnu/ld-2.19.so)
==22866== 
==22866== LEAK SUMMARY:
==22866==    definitely lost: 0 bytes in 0 blocks
==22866==    indirectly lost: 0 bytes in 0 blocks
==22866==      possibly lost: 1,352 bytes in 18 blocks
==22866==    still reachable: 45,900 bytes in 222 blocks
==22866==                       of which reachable via heuristic:
==22866==                         newarray           : 1,536 bytes in 16 blocks
==22866==         suppressed: 0 bytes in 0 blocks
==22866== 
==22866== For counts of detected and suppressed errors, rerun with: -v
==22866== ERROR SUMMARY: 3 errors from 3 contexts (suppressed: 0 from 0)

显示的大多数记录(但是1)是"仍然可到达的块".我读过这些可能是由于延迟池释放造成的.即在我的main(argc,argv){}函数终止后,容器的释放(例如我使用很多的向量).

然而,确实存在一些问题,因为在执行大约8-9个小时之后,我清楚地看到可执行文件使用的内存比开始时多(在我的电脑中它开始时使用0.6%的内存,并且在8小时后它使用了大约6个内存) %).

问题是这些是神秘的消息 - 绝对没有源自我的源文件.我在这里读到的"_dl*"调用与linux loader有关.那么如何确定问题的来源呢?

我应该补充说,这段代码使用了

    1 + 3个线程(用于所有阻塞操作,即从stdin和串口读取并将数据写入文件),

    boost库(特别是循环缓冲区)和

    gsl库.

然而,我已经从小概念证明部分构建了代码,这些部分在valgrind中没有显示任何错误/警告.

另外,我的代码中只有有限数量的指向类对象的指针,我delete在相应的析构函数中验证了这一点.



1> Sam Varshavc..:

原始分配调用来自共享库初始化代码,当应用程序链接的共享库在运行时加载时,通常在任何应用程序代码实际运行之前执行.这就是您在回溯中看不到代码的原因.它甚至没有运行.

要查找的关键符号是_dl_init共享库初始化的入口点.从上游看,它会告诉您正在初始化哪个库.在你的情况下,它是一堆Gnome库和一个名为"libpixman"的库.

共享库还具有清理功能,可在卸载共享库时调用该功能.

组织良好的共享库将使用共享库清理功能有序地释放它在启动时分配的所有内存.遗憾的是,这种对细节的疏忽是非常常见的:共享库为堆分配一堆内存,用于共享库的内部静态表,而无需在共享库卸载时解除分配该内存.

在您的应用程序运行时,这不太可能导致您正在观察的内存泄漏,除非在一个案例中,我将在稍后提及.根据我的经验,这种草率分配实践仅用于共享库在加载时分配一次的静态表.这里的想法是没有必要在自己之后明确地清理,因为只有当进程退出时,库才会被卸载一次.

可悲的是,削减这些角落的开发人员从未听说过dlopen()和dlclose().这使得大型应用程序无法仅在需要时加载共享库并在之后卸载它,直到需要再次使用它.

因此,除非您的应用程序代码重复dlopen()和dlclose()所有这些Gnome库和libpixman,否则您将不得不继续在其他地方查找泄漏.您应该阅读并使用valgrind的抑制文件来抑制其输出中的这种烦人的噪音.


"cli application"是一个毫无意义的术语,并不重要.唯一重要的是你要链接的库.无论您要链接哪些库,它们都不需要自己只与libc链接.他们可以链接到任何其他库.因此,如果您没有直接链接到任何gnome库,那么您链接的任何库都必须直接或间接链接到gnome库.即使您的代码不直接使用任何需要gnome的资源,只要您使用的库与gnome链接,它就会被加载.
推荐阅读
携手相约幸福
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有