【问题标题】:gcc - how to use address sanitizergcc - 如何使用地址清理器
【发布时间】:2018-04-22 09:39:12
【问题描述】:

我在 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")

链接库:

target_link_libraries(testcpp asan)

有内存泄漏的测试程序:

int main()
{
    int *prt = new int;
    return 0;
}

怎么了?

【问题讨论】:

  • 直接在命令行编译你的程序(cmake只是让我们感到困惑)。在您的问题中显示编译命令。
  • BTW GCC4.8 已经很老了,而且它的地址清理器已经取得了很大的进步(特别是在 GCC 5 和 GCC 6 中)。尝试使用更新的 GCC(如果可能,使用 GCC 7)
  • 我的理解是,如果你已经指定了-fsanitize=address,就不需要链接asan。此外,您必须实际运行程序才能运行消毒剂。
  • 也许你想要-fsanitize=leakgcc.gnu.org/onlinedocs/gcc-6.3.0/gcc/…

标签: c++ linux gcc memory-leaks


【解决方案1】:

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

// file irbis.cc
int main()
{
  int *prt = new int;
  return 0;
}

使用

g++ -fsanitize=address -g3 -std=c++11 irbis.cc -o irbis

在执行./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).

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

【讨论】:

  • 我并不感到惊讶。您需要最新的 GCC,而 4.8 不是。
猜你喜欢
  • 2019-02-07
  • 2022-06-13
  • 2016-04-21
  • 2018-05-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-22
相关资源
最近更新 更多