3

I have a project which built by cmake. I want to build it with AddressSanitizer to detect memory leaks. I added these lines into the CMakeLists.txt:

set (CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -fno-omit-frame-pointer -fsanitize=address")
set (CMAKE_LINKER_FLAGS_DEBUG "${CMAKE_STATIC_LINKER_FLAGS_DEBUG} -fno-omit-frame-pointer -fsanitize=address")

But it doesnt work some how (It doesnt show where i have memory leaks). I tested with a test application which definitely contains memory leaks, still nothing happened. Can somebody explain how should i do it ?

Tuấn Phạm
  • 688
  • 6
  • 20

2 Answers2

4

As an other option, it's possible to add sanitizers to target:

target_link_libraries(MyTarget PRIVATE -fsanitize=address)

Got from this answer, tested with cmake 3.6

PolyGlot
  • 740
  • 6
  • 11
1

try flags without _DEBUG.

    set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-omit-frame-pointer -fsanitize=address")
    set (CMAKE_LINKER_FLAGS "${CMAKE_LINKER_FLAGS} -fno-omit-frame-pointer -fsanitize=address")
Maksym Rudenko
  • 706
  • 5
  • 16