【发布时间】:2020-01-18 04:16:46
【问题描述】:
我有一个内存泄漏的示例代码。尽管 clang 正确显示了泄漏,但我无法使用 gcc 实现相同的目的。我使用的 gcc 版本是 4.8.5-39
代码:
#include <stdlib.h>
void *p;
int main() {
p = malloc(7);
p = 0; // The memory is leaked here.
return 0;
}
叮当声:
clang -fsanitize=address -g memory-leak.c ; ASAN_OPTIONS=detect_leaks=1 ./a.out
=================================================================
==15543==ERROR: LeakSanitizer: detected memory leaks
Direct leak of 7 byte(s) in 1 object(s) allocated from:
#0 0x465289 in __interceptor_malloc (/u/optest/a.out+0x465289)
#1 0x47b549 in main /u/optest/memory-leak.c:4
#2 0x7f773fe14544 in __libc_start_main /usr/src/debug/glibc-2.17-c758a686/csu/../csu/libc-start.c:266
SUMMARY: AddressSanitizer: 7 byte(s) leaked in 1 allocation(s).
海合会:
gcc -fsanitize=address -g memory-leak.c ; ASAN_OPTIONS=detect_leaks=1 ./a.out
我需要使用 gcc。有人可以帮我理解为什么 gcc 的行为不像 clang 以及我应该怎么做才能让它工作。
【问题讨论】:
-
用较不古老的 gcc 版本再试一次,它对我有用。
标签: gcc memory-leaks clang address-sanitizer