如何使用 Leak Sanitizer 查找内存泄漏的原因

How to find reason of memory leak with Leak Sanitizer

我有一个使用 tbb 的 C++ 程序,我正在使用 GCC 6.2.1 在 64 位 Linux 上编译。 当我使用 address sanitizer(-fsanitize=address) 和 运行 单元测试进行编译时,会生成以下输出:

...
[  PASSED  ] 56 tests.

=================================================================
==12326==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 54 byte(s) in 3 object(s) allocated from:
    #0 0x7f4c634fd020 in strdup (/usr/lib64/libasan.so.3+0x58020)
    #1 0x301d215bb4  (/usr/lib64/libtbb.so.2+0x301d215bb4)

SUMMARY: AddressSanitizer: 54 byte(s) leaked in 3 allocation(s).
make[3]: *** [CMakeFiles/check] Error 1
make[2]: *** [CMakeFiles/check.dir/all] Error 2
make[1]: *** [CMakeFiles/check.dir/rule] Error 2
make: *** [check] Error 2

代码是在关闭优化 (-O0) 和 -fno-omit-frame-pointer 的情况下编译的。我怎样才能获得有关泄漏的更多信息?

泄漏发生在一个系统库中,该库可能是在没有 -fno-omit-frame-pointer 的情况下编译的,因此 Asan 无法使用帧指针展开它。您可以通过设置

尝试使用较慢但更强大的 DWARF 展开器
# Or LSAN_OPTIONS, if you use standalone LSan
export ASAN_OPTIONS=fast_unwind_on_malloc=0

有关运行时标志的更多详细信息,请参阅 here and here

顺便说一句,您可以通过

要求 LSan 在发生错误时不要中止
# Or LSAN_OPTIONS, if you use standalone LSan
export ASAN_OPTIONS=exitcode=0:...