修改cmakelist.txt,在project(xxxx)后面追加:
option(MEM_CHECK "memory check with AddressSanitizer" OFF)
if(MEM_CHECK)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address")
message(STATUS "Build with memory check")
endif()
编译追加cmake参数-DMEM_CHECK=ON:
cmake -DMEM_CHECK=ON
执行程序:
ASAN_OPTIONS=detect_leaks=1 ASAN_OPTIONS=detect_container_overflow=0 ./xxxx
发现问题时,程序会停下来并在终端报告问题,