gcc - 如何使用地址清理器

Irb*_*bis 7 c++ linux gcc memory-leaks

我在 linux 上使用 gcc 4.8.5。我想使用地址消毒剂,但它不返回有关该程序的任何信息。标志:

SET(CMAKE_CXX_FLAGS "-Wall -Wno-error -g -std=c++11 -fno-omit-frame-pointer -fsanitize=address")
SET(CMAKE_LINKER_FLAGS "${CMAKE_LINKER_FLAGS} -fno-omit-frame-pointer -fsanitize=address")
Run Code Online (Sandbox Code Playgroud)

链接库:

target_link_libraries(testcpp asan)
Run Code Online (Sandbox Code Playgroud)

有内存泄漏的测试程序:

int main()
{
    int *prt = new int;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

怎么了 ?

Bas*_*tch 8

在最近的 Debian/Sid/x86-64 上使用 GCC7 我编译了这个

// file irbis.cc
int main()
{
  int *prt = new int;
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

使用

g++ -fsanitize=address -g3 -std=c++11 irbis.cc -o irbis
Run Code Online (Sandbox Code Playgroud)

并在执行时./irbis正确检测到泄漏:

=================================================================
==22742==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 4 byte(s) in 1 object(s) allocated from:
    #0 0x7f77ea911340 in operator new(unsigned long) 
            (/usr/lib/x86_64-linux-gnu/libasan.so.4+0xdb340)
    #1 0x55ea91cca81b in main /home/basile/tmp/irbis.cc:4
    #2 0x7f77e9c1f2e0 in __libc_start_main 
            (/lib/x86_64-linux-gnu/libc.so.6+0x202e0)

SUMMARY: AddressSanitizer: 4 byte(s) leaked in 1 allocation(s).
Run Code Online (Sandbox Code Playgroud)

所以升级你的 GCC 编译器(至少到 GCC6)。我确实知道 GCC4.8 对地址清理器和 C++11 的支持不完整(顺便说一句,GCC4.8 已过时,GCC5 也已过时,2017 年 11 月)。

  • 我不惊讶。您需要最新的 GCC,而 4.8 不是。 (2认同)