I'm working and on a large C++ project and making it compile with clang would be painful, so I'm stuck with GCC.
I want to use the nice -fsanitize=leak
flag that I already used with clang on a previous job, but it does not seem to work.
I made a very simple example to test it:
#include <stdlib.h>
void FooBar() {
malloc(7);
}
int main() {
FooBar();
return 0;
}
With clang it works as expected:
>> clang -fsanitize=leak main.cpp
>> LSAN_OPTIONS=detect_leaks=1 ./a.out
=================================================================
==18052==ERROR: LeakSanitizer: detected memory leaks
Direct leak of 7 byte(s) in 1 object(s) allocated from:
#0 0x41dcbc (~/dev/addresssanitizertest/a.out+0x41dcbc)
#1 0x431ac3 (~/dev/addresssanitizertest/a.out+0x431ac3)
#2 0x431ae3 (~/dev/addresssanitizertest/a.out+0x431ae3)
#3 0x7f8077e71a3f (/lib/x86_64-linux-gnu/libc.so.6+0x20a3f)
SUMMARY: LeakSanitizer: 7 byte(s) leaked in 1 allocation(s).
>>
But with gcc it does not seem to detect anything:
>> gcc -fsanitize=leak main.cpp
>> LSAN_OPTIONS=detect_leaks=1 ./a.out
>>
Did I miss something like a nice environment variable? Did someone ever made it work with gcc?
EDIT: This works for instance:
g++ -fsanitize=address main.cpp
ASAN_OPTIONS=detect_leaks=1 ./a.out
But I can't do that: the perf drawback is too much. I only want leak detection.
You must read this and use the patch :