1

I am now running CTest with or without Valgrind in Ubuntu Linux. Firstly, I set up a CMakeLists.txt script to enable testing:

enable_testing()
include(CTest)
if(UNIX)
  set(CTEST_MEMORYCHECK_COMMAND, "usr/bin/valgrind")
  set(CTEST_MEMORYCHECK_COMMAND_OPTIONS, "--trace-children=yes --leak-check=full")
endif()

add_test(NAME test
        WORKING_DIRECTORY ${my_outputdirectory}
        COMMAND test_exe)

When I run the test without valgrind, I use the following command:

cmake -G "CodeBlocks - Unix Makefiles"
ctest -D ExperimentalBuild  
ctest -R test -D ExperimentalTest  

That works fine. However, when I run the following command to invoke valgrind:

 cmake -G "CodeBlocks - Unix Makefiles"
    ctest -D ExperimentalBuild  
    ctest -R test -D ExperimentalMemChec

the following message appear:

--Processing memory checking output:
Memory checking results:

This is definitely not the diagnostic information I expect. I was wondering what I should do next. Thanks!

EDIT: Later on, I find that the diagnostic information can be available only in the case where the memory leak happens. However, the diagnostic information is very vague in the sense that the location where the error occurs is not given. How could I obtain more detailed information?

feelfree
  • 11,175
  • 20
  • 96
  • 167

3 Answers3

2

By default, CMake does not build debug symbols for Makefile projects, so Valgrind is unable to determine the exact location of a leak in source code.

Try running cmake with

cmake -DCMAKE_BUILD_TYPE=Debug /path/to/source

which should add the compiler option for building debug symbols.

ComicSansMS
  • 51,484
  • 14
  • 155
  • 166
  • Debug is one of the build types defined in your cache file, it just sends a certain additional string when you use it. How it's defined may vary, you can edit it. – Alan Corey Sep 20 '19 at 22:16
1

I use a python script which parses my memory leaks from valgrind it is available here.

In CMake I use the following command to add a memory test:

ADD_TEST(testName ${Test_Dir}/memtest.py ${CMAKE_CURRENT_BINARY_DIR}/testExecutable ${CMAKE_BINARY_DIR})

Such that I do not need to parse the memory leak errors direct in cmake. The python script simply executes a memory check with valgrind on the executable and returns an error if a leak was found. If a leak was found the test then fails otherwise it passes. Hope this might help you.

tune2fs
  • 7,605
  • 5
  • 41
  • 57
1

CMake by default uses the following command line arguments for valgrind memcheck:

--log-file=/Path/to/build-dir/Testing/Temporary/MemoryChecker.1.log \
-q --tool=memcheck --leak-check=yes --show-reachable=yes --num-callers=50

Note that the --log-file argument means that any valgrind errors ends up in that file. I find it more useful when valgrind posts the information to the stderr, so a build server like Jenkins or TeamCity can show it more easily. In order to do that, you have to set the MEMORYCHECK_COMMAND_OPTIONS variable with --log-fd=2 (and other options if you'd like) so it overrides the --log-file option. More info here.

dcmm88
  • 1,445
  • 1
  • 12
  • 30