1

I am trying to run a simple leak check program.

#include <iostream>

int main()
{
   
    double *ptr = new double(3.14);


}  

using the command g++ -g -fsanitize=leak -o main main.cpp

and I get the following error:

clang: error: unsupported option '-fsanitize=leak' for target 'x86_64-apple-darwin20.1.0'

I stopped using the clang that comes with Xcode and installed clang/LLVM using homebrew.

$ which clang++
/usr/local/opt/llvm/bin/clang++
clang++ --version
clang version 11.0.0
Target: x86_64-apple-darwin20.1.0
Thread model: posix
InstalledDir: /usr/local/opt/llvm/bin

EDIT: When I was using apple clang, g++ used to default to clang++. Apparently that changed when I installed llvm/clang. Thanks to @cigien for pointing it out. g++ still uses default to the compiler that that comes with Apple clang.

 g++ --version
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/4.2.1
Apple clang version 12.0.0 (clang-1200.0.32.27)
Target: x86_64-apple-darwin20.1.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
Morpheus
  • 3,285
  • 4
  • 27
  • 57
  • You're showing `which clang++`, but compiling with `g++`. Are they aliases on your system? – cigien Dec 02 '20 at 16:32
  • When I was using apple clang, g++ used to default to clang++. Apparently that changed when I installed llvm/clang. Thanks – Morpheus Dec 02 '20 at 16:34
  • System g++ is an alias for system clang++ on macs, but a weird one, as I believe it also uses and outdated Standard LIbrary. I recommend avoiding it at all costs. – sweenish Dec 02 '20 at 16:35
  • Thanks for the edit, but it's still unclear. If they're not aliases, then showing `which clang++` is meaningless if you're compiling with `g++`. Please clarify the question. – cigien Dec 02 '20 at 16:37

1 Answers1

3

According to this answer you should use:
g++-10 -g -fsanitize=leak -o main main.cpp (in this cases (with flag leak) there is no leak message for me ./main, but it compile) then it should be:
g++-10 -fsanitize=address -g main.cpp ; ASAN_OPTIONS=detect_leaks=1 ./a.out and it detect leak well.

  • Note, that the correct path of brew installed g++ in MacOS is:
$ which g++-10
> /usr/local/bin/g++-10
--
$ which g++ 
> /usr/bin/g++ //this is pseudonym of clang

The same for gcc-10 (10 is my current version. You should use your version instead of that)


If you use CMakeLists.txt file you will configure it like this:

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=[sanitizer_name] [additional_options] [-g] [-OX]") 
# leak sanitizer_name not works for me. should be address

And should execute cmake command like this: cmake -DCMAKE_C_COMPILER=/usr/local/bin/gcc-10 -DCMAKE_CXX_COMPILER=/usr/local/bin/g++-10 .. And then ASAN_OPTIONS=detect_leaks=1 ./a.out

Note, that if you open */CMakeFiles/3.18.4/CMakeCXXCompiler.cmake file you will observe the compiled info, and now it will be g++.

0x0000dead
  • 330
  • 1
  • 4
  • 12