【发布时间】:2023-01-29 20:06:12
【问题描述】:
你好 stackoverflow 社区,我正在创建内存泄漏以使用 ASAN 并检测内存泄漏。
$ export MallocNanoZone='0' # to avoid https://stackoverflow.com/q/64126942/9497703 on OS X
$ cat new_delete.cc
class Dummy {
public:
Dummy(int x) {
sz = x;
}
private:
int sz;
};
void func(int i) {
Dummy* p = new Dummy(i);
if (i < 50) {
// If return from here then there is a memory leak on the
// heap. Dummy is not freed.
return;
}
// Do useful things.
delete p;
}
int main() {
func(10);
}
$ clang++ -fsanitize=address -g -O0 new_delete.cc
$ ./a.out
我期待 ASAN 检测到这个内存泄漏。然而,它没有。
谁能指出我在这里缺少什么?我正在使用 OS X 和以下 clang 版本:
$ clang++ --version
Apple clang version 12.0.0 (clang-1200.0.32.28)
Target: x86_64-apple-darwin21.6.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
【问题讨论】: