【发布时间】:2015-11-05 11:43:06
【问题描述】:
我有一种奇怪的情况,即完全相同的可执行文件的 Valgrind 输出在一台机器上与另一台机器上不同。
我正在用 C++ 编写一个 Windows 注册表读/写库,并且我一直在努力编写代码时非常健壮。所以我一直在运行 Valgrind 并修复内存泄漏,因为它们是通过一组单元测试生成的可执行文件引入的。
我的主要开发 PC 正在运行 Linux Mint Debian Edition x86_64。一个周末,我将存储库克隆到运行 Arch x86_64 的笔记本电脑上,并且我开始在 Valgrind 中出现内存泄漏(或者更确切地说,仍然可以访问的块)。在拉了很多头发之后,我意识到即使是在 Mint DE 上干净的最后一次提交也在 Arch 上显示出泄漏。我还注意到 Arch 盒子上分配的数量超过了四倍,尽管我不知道这是否意味着什么。
这是我在两台机器上使用相同 exe 的内容(在一台机器上编译并复制到另一台机器上):
薄荷 DE x86_64:
valgrind --leak-check=full --show-leak-kinds=all ./regnixtest
==12248== Memcheck, a memory error detector
==12248== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==12248== Using Valgrind-3.10.0 and LibVEX; rerun with -h for copyright info
==12248== Command: ./regnixtest
==12248==
Testing Create Valid NK...passed
[ normal command output snipped for brevity ]
==12248==
==12248== HEAP SUMMARY:
==12248== in use at exit: 0 bytes in 0 blocks
==12248== total heap usage: 603 allocs, 603 frees, 608,657,070 bytes allocated
==12248==
==12248== All heap blocks were freed -- no leaks are possible
==12248==
==12248== For counts of detected and suppressed errors, rerun with: -v
==12248== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
Arch x86_64:
valgrind --leak-check=full --show-leak-kinds=all ./regnixtest
==5006== Memcheck, a memory error detector
==5006== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==5006== Using Valgrind-3.10.1 and LibVEX; rerun with -h for copyright info
==5006== Command: ./regnixtest
==5006==
Testing Create Valid NK...passed
[ normal command output snipped for brevity ]
==5006==
==5006== HEAP SUMMARY:
==5006== in use at exit: 72,704 bytes in 1 blocks
==5006== total heap usage: 2,927 allocs, 2,926 frees, 608,844,359 bytes allocated
==5006==
==5006== 72,704 bytes in 1 blocks are still reachable in loss record 1 of 1
==5006== at 0x4C29F90: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==5006== by 0x4EC01EF: pool (eh_alloc.cc:117)
==5006== by 0x4EC01EF: __static_initialization_and_destruction_0 (eh_alloc.cc:244)
==5006== by 0x4EC01EF: _GLOBAL__sub_I_eh_alloc.cc (eh_alloc.cc:307)
==5006== by 0x400F0E9: call_init.part.0 (in /usr/lib/ld-2.21.so)
==5006== by 0x400F1FA: _dl_init (in /usr/lib/ld-2.21.so)
==5006== by 0x4000DB9: ??? (in /usr/lib/ld-2.21.so)
==5006==
==5006== LEAK SUMMARY:
==5006== definitely lost: 0 bytes in 0 blocks
==5006== indirectly lost: 0 bytes in 0 blocks
==5006== possibly lost: 0 bytes in 0 blocks
==5006== still reachable: 72,704 bytes in 1 blocks
==5006== suppressed: 0 bytes in 0 blocks
==5006==
==5006== For counts of detected and suppressed errors, rerun with: -v
==5006== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
这是我 Arch 盒子上的一个库的问题吗?以下是每个 ldd 的输出,以防万一:
薄荷 DE x86_64:
ldd ./regnixtest
linux-vdso.so.1 (0x00007ffc76ffc000)
libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f080dcaa000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f080d9a9000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f080d792000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f080d3e9000)
/lib64/ld-linux-x86-64.so.2 (0x00007f080dfd2000)
Arch x86_64:
ldd ./regnixtest
linux-vdso.so.1 (0x00007ffd81bd5000)
libstdc++.so.6 => /usr/lib/libstdc++.so.6 (0x00007fcf9d788000)
libm.so.6 => /usr/lib/libm.so.6 (0x00007fcf9d484000)
libgcc_s.so.1 => /usr/lib/libgcc_s.so.1 (0x00007fcf9d26e000)
libc.so.6 => /usr/lib/libc.so.6 (0x00007fcf9cecc000)
/lib64/ld-linux-x86-64.so.2 (0x00007fcf9db0a000)
这个项目最终将是开源的,但我还没有准备好将代码放在那里。如果有帮助,我可以继续将它放在 GitHub 上。
【问题讨论】:
-
在撰写本文时,Mint 拥有 gcc 4.x,而 arch 拥有 gcc 5.2,因此存在差异。使用 g++ 编译并在 arch 上使用相同的 valgrind 选项运行的简单
int main(void) { return 0; }显示相同的“泄漏”。如果您担心噪音会隐藏真正的问题,只需为此警告添加一个抑制。 -
@richq 那么问题可能出在 libgcc 上?我认为它必须在某个库中,因为在 arch 上编译的二进制文件在 mint 上没有问题。看起来您对该测试代码的结果回答了我的问题(只要我知道我的代码正确释放了东西,我就不担心哪个系统库有问题)。如果您愿意,您可以将您的评论转换为答案,我可以接受。
标签: c++ linux memory-leaks valgrind