我不知道如何在使用 CLion 的 MacOS Big Sur 上使用 CLion 查看内存泄漏,我已经尝试过以下操作:
Valgrind - 与 Big Sur 不兼容
来自 Clang 的 Leak Sanitizer - 据 CLion 的支持人员称,它显然与 MacOS 不兼容
在 CLion 内部,我在 CMakeLists.txt 中编写了以下命令:
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address -g")
Run Code Online (Sandbox Code Playgroud)
然后我在“首选项”菜单 ->“地址消毒剂”部分的消毒剂中写入:
detect_stack_use_after_return=1
Run Code Online (Sandbox Code Playgroud)
根据 CLion 支持页面,他们说 Leak Sanitizer 包含在 Address sanitizer 中。
我从LeakSanitizer工具获得以下输出。正如工具所了解的那样,直接泄漏和间接泄漏有什么区别?
13: ==29107==ERROR: LeakSanitizer: detected memory leaks
13:
13: Direct leak of 288 byte(s) in 6 object(s) allocated from:
13: #0 0x7f2ce0089050 in __interceptor_malloc (/nix/store/zahs1kwq4742f6l6h7yy4mdj44zzc1kd-gcc-7-20170409-lib/lib/libasan.so+0xd9050)
13: #1 0x7f2cdfb974fe in qdr_core_subscribe ../src/router_core/route_tables.c:149
13: #2 0x7f2cdfb47ff0 in IoAdapter_init ../src/python_embedded.c:548
13: #3 0x7f2cde966ecd in type_call (/nix/store/1snk2wkpv97an87pk1842fgskl1vqhkr-python-2.7.14/lib/libpython2.7.so.1.0+0x9fecd)
13:
13: Indirect leak of 2368 byte(s) in 1 object(s) allocated from:
13: #0 0x7f2ce0089b88 in __interceptor_posix_memalign (/nix/store/zahs1kwq4742f6l6h7yy4mdj44zzc1kd-gcc-7-20170409-lib/lib/libasan.so+0xd9b88)
13: #1 0x7f2cdfbcc8ea in qd_alloc ../src/alloc_pool.c:182
13: #2 0x7f2cdfbb6c6b in qd_server_connection ../src/server.c:500
13: #3 0x7f2cdfbbe27d in on_accept ../src/server.c:531
13: …
Run Code Online (Sandbox Code Playgroud) 当使用带有 gcc 的消毒剂时,可以提供一个异常/抑制列表来处理误报等。
抑制文件格式记录不全。
每个抑制的形式都是
name_of_check:path_or_name
Run Code Online (Sandbox Code Playgroud)
的有效值是name_of_check
什么?
gcc sanitizer thread-sanitizer address-sanitizer leak-sanitizer
GCC 和 Clang 编译器都支持LeakSanitizer,这有助于查找 C 程序中的内存泄漏。有时内存泄漏是不可避免的(例如,因为它正在测试套件中进行测试)。
可以使用Leak Sanitizer 接口对此类内存进行注释:
#include <sanitizer/lsan_interface.h>
void *p = create_new_object();
__lsan_ignore_object(p);
Run Code Online (Sandbox Code Playgroud)
然而,这会在不支持 LSan 的编译器上崩溃。在 Address Sanitizer 中,此构造可用于检测 ASAN 的可用性:
/* __has_feature(address_sanitizer) is used later for Clang, this is for
* compatibility with other compilers (such as GCC and MSVC) */
#ifndef __has_feature
# define __has_feature(x) 0
#endif
#if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__)
/* ASAN-aware code here. */
#endif
Run Code Online (Sandbox Code Playgroud)
仅在 Clang 中检测不到__has_feature(leak_sanitizer)
LSan 的存在,__SANITIZE_LEAKS__
在 GCC 中也检测不到 LSan 的存在。我怎样才能检测 ASAN 的可用性?请注意,LSan 可以独立于 …
我遇到一个问题,任何通过动态加载库的 Leak Sanitizer 回溯都会报告Unknown Module
该库中的任何函数调用。
Direct leak of 48 byte(s) in 1 object(s) allocated from:
#0 0x4e3e36 in malloc (/usr/sbin/radiusd+0x4e3e36)
#1 0x7fb406e95f69 (<unknown module>)
#2 0x7fb406eafc36 (<unknown module>)
#3 0x7fb406eafd40 (<unknown module>)
#4 0x7fb406ea3364 (<unknown module>)
#5 0x7fb4063de7d4 (<unknown module>)
#6 0x7fb4063c61c4 (<unknown module>)
#7 0x7fb406617863 (<unknown module>)
#8 0x7fb415620681 in dl_load_func /usr/src/debug/freeradius-server-4.0.0/src/main/dl.c:194:34
#9 0x7fb41561edab in dl_symbol_init_walk /usr/src/debug/freeradius-server-4.0.0/src/main/dl.c:301:7
#10 0x7fb41561df1e in dl_module /usr/src/debug/freeradius-server-4.0.0/src/main/dl.c:748:6
#11 0x7fb41561f3db in dl_instance /usr/src/debug/freeradius-server-4.0.0/src/main/dl.c:853:20
#12 0x7fb41564f4ab in module_bootstrap /usr/src/debug/freeradius-server-4.0.0/src/main/module.c:827:6
#13 0x7fb41564ed56 in modules_bootstrap /usr/src/debug/freeradius-server-4.0.0/src/main/module.c:1070:14 …
Run Code Online (Sandbox Code Playgroud) 当我用 编译我的 C++ 代码时-fsanitize=address
,我的软件会在它退出时打印出一个泄漏列表。有没有办法避免泄漏报告(我只对内存损坏感兴趣,而不是泄漏)?我去了带有ASAN flags page 的页面,但看起来这些标志中的任何一个都不匹配。
阅读完这个 StackOverflow 问题后:直接泄漏和间接泄漏有什么区别?我留下的印象是,如果我修复所有直接泄漏(多次修复测试通过,因为在修复之前的直接泄漏后间接泄漏可能会变成直接泄漏),最终我会得到 0 次泄漏。
我目前正在使用 Leak Sanitizer (LSAN),在修复了所有直接泄漏(因此一些间接泄漏消失了)之后,我现在留下了一堆间接泄漏。为什么没有直接的?这什么时候会发生?如何诊断和修复剩余的泄漏?
我有一个最小的GStreamer程序:
#include <gst/gst.h>
int main() {
gst_init(NULL, NULL);
gst_deinit();
}
Run Code Online (Sandbox Code Playgroud)
我使用gcc test.c $(pkg-config --cflags --libs gstreamer-1.0) -fsanitize=address
(gcc 版本为 12.1.0)构建它,运行它并从地址清理器获得以下输出:
==87326==ERROR: LeakSanitizer: detected memory leaks
Direct leak of 16384 byte(s) in 1 object(s) allocated from:
#0 0x7f53e28bfa89 in __interceptor_malloc /usr/src/debug/gcc/libsanitizer/asan/asan_malloc_linux.cpp:69
#1 0x7f53e26c1b19 in g_malloc (/usr/lib/libglib-2.0.so.0+0x5db19)
SUMMARY: AddressSanitizer: 16384 byte(s) leaked in 1 allocation(s).
Run Code Online (Sandbox Code Playgroud)
我是 GStreamer 和 GLib 的新手。这对于 GStreamer 程序来说正常吗?如果是的话,在使用消毒剂运行单元测试时,有什么优雅的方法可以忽略这种泄漏?