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

3

10

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.

Kobold answered 12/3, 2021 at 12:4 Comment(1)
Try looking for answers for CMake. For example, I like the "Create Asan profile" in this question: #44320965 - I could see it in CLion.Nicolettenicoli
G
4

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]
Gustafsson answered 28/7, 2021 at 1:22 Comment(0)
C
1

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

Cobra answered 20/10, 2021 at 15:54 Comment(0)
N
0

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

Newton answered 12/3, 2021 at 12:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.