How can I see memory leaks on MacOS Big Sur using CLion?

4.6k views Asked by At

I don't know how can I see memory leaks using CLion on MacOS Big Sur using CLion and I've tried these things:

  1. Valgrind - which is not compatible with Big Sur

  2. Leak Sanitizer from Clang - which apparently isn't compatible with MacOS according to a support guy from CLion

  3. Inside CLion, I've written in CMakeLists.txt this command:

       set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address -g")
    

    then I've written in preferences menu -> sanitizers in Address sanitizer section:

       detect_stack_use_after_return=1
    

    Based on CLion support page, they said that Leak Sanitizer is included in Address sanitizer.

3

There are 3 answers

0
ralf htp On

Normally you could use libasan during compilation (https://www.osc.edu/resources/getting_started/howto/howto_use_address_sanitizer) however on MacOS there are some extra steps :

https://clang.llvm.org/docs/AddressSanitizer.html (search this page for 'MacOS' to get an overview) :

Memory leak detection

For more information on leak detector in AddressSanitizer, see LeakSanitizer. The leak detection is turned on by default on Linux, and can be enabled using ASAN_OPTIONS=detect_leaks=1 on macOS; however, it is not yet supported on other platforms.

source : https://clang.llvm.org/docs/AddressSanitizer.html

Also see Mac OS: Leaks Sanitizer and https://developer.apple.com/documentation/xcode/diagnosing_memory_thread_and_crash_issues_early

0
Charles On

Assuming you have installed the xcode command line developer tools, open a terminal window in CLion and try the following command, where programname is the name of the program you are building:

leaks -atExit -- cmake-build-debug/programname

You get output something like this:

leaks Report Version: 4.0
Process 69522: 214 nodes malloced for 21 KB
Process 69522: 1 leak for 1008 total leaked bytes.

    1 (1008 bytes) ROOT LEAK: 0x14c6067f0 [1008]
0
Alexander Yalunin On

In Preferences | Build, Execution, Deployment | CMake -> Cmake options use

-DCMAKE_BUILD_TYPE=ASAN -DCMAKE_CXX_COMPILER=/usr/local/opt/llvm/bin/clang++

and

enter image description here