You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何判定Valgrind检测到的libc++内存泄漏是否为误报?

关于libc++下Valgrind检测到的内存泄漏疑问

我编写了一个简单的C++程序,采用两种配置构建:

  • 链接libstdc++
  • 链接libc++

使用Valgrind执行以下命令运行两个版本:

valgrind --leak-check=full --show-reachable=yes --track-origins=yes --log-file=test_program.log -v ./test_program

链接libstdc++版本的检测结果

该版本运行后无内存泄漏:

==
== HEAP SUMMARY:
==     in use at exit: 0 bytes in 0 blocks
==   total heap usage: 24,813,106 allocs, 24,813,106 frees, 51,325,970,073 bytes allocated
==
== All heap blocks were freed -- no leaks are possible
==
== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

链接libc++版本的检测结果

该版本被检测到内存泄漏:

==434036== HEAP SUMMARY:
==434036==     in use at exit: 16 bytes in 1 blocks
==434036==   total heap usage: 317,709,577 allocs, 317,709,576 frees, 645,827,127,171 bytes allocated
==434036==
==434036== Searching for pointers to 1 not-freed blocks
==434036== Checked 401,408 bytes
==434036==
==434036== 16 bytes in 1 blocks are still reachable in loss record 1 of 1
==434036==    at 0x484DA83: calloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==434036==    by 0x49A365F: ??? (in /usr/lib/llvm-14/lib/libc++abi.so.1.0)
==434036==    by 0x49A24E9: __cxa_get_globals (in /usr/lib/llvm-14/lib/libc++abi.so.1.0)
==434036==    by 0x49A53F6: __cxa_throw (in /usr/lib/llvm-14/lib/libc++abi.so.1.0)
==434036==    by 0x2EE7B3: goal::details::special_node<double>::value() const (in workspace/goal/goal_test)
==434036==    by 0x2DC349: goal::details::caller_node<double>::value() const (in workspace/goal/goal_test)
==434036==    by 0x50AC40: double goal::details::arg_node<double>::process<main_node<double> > const&) (in workspace/goal/goal_test)
==434036==    by 0x45A79C: bool execute_test_base<double>() (in workspace/goal/goal_test)
==434036==    by 0x2322A0: main (in workspace/goal/goal_test)
==434036==
==434036== LEAK SUMMARY:
==434036==    definitely lost: 0 bytes in 0 blocks
==434036==    indirectly lost: 0 bytes in 0 blocks
==434036==      possibly lost: 0 bytes in 0 blocks
==434036==    still reachable: 16 bytes in 1 blocks
==434036==         suppressed: 0 bytes in 0 blocks
==434036==
==434036== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

Sanitizer检测情况

我还构建了另外两个版本:

  • 链接libstdc++并启用ASAN/LSAN/UBSAN/TSAN
  • 链接libc++并启用ASAN/LSAN/UBSAN/TSAN

运行这两个版本时,均未触发任何sanitizer的错误或警告。

使用的编译器

  • g++-13 (Ubuntu 13.1.0-8ubuntu1~22.04) 13.1.0
  • clang version 20.0.0git

仅当链接libc++时,两种编译器下Valgrind都会检测到该泄漏。

疑问

Valgrind检测到的该泄漏是否为误报?还可通过哪些方法验证其真实性?


内容的提问来源于stack exchange,提问作者Magnus Man

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 00:45:55