Search code examples
c++cmakectest

How to test using CMake that a C++ application has no memory errors?


I have a C++ project in CMake and I have implemented unit tests (using GoogleTest) as well as integration tests i.e. executing applications and checking the results using regexp e.g.

add_test(NAME MyAppIntegration.testRefitRunMse COMMAND my_application "--refit=true")
set_tests_properties(MyAppIntegration.testRefitRunMse PROPERTIES PASS_REGULAR_EXPRESSION "mse\\=1\\.53611e\\-05")

Now I would like to do the same as above but running the application to detect memory errors e.g. simply run ./my_application and check for memory errors with valgrind. How can I do that? Note that I am aware of ctest -D ExperimentalMemCheck and ctest -T memcheck but here I end up with the error Cannot find file DartConfiguration.tcl which seems related to the Dashboard configuration that I don't need.


Solution

  • OK I found a possible solution. Adding these lines to my CMakeLists.txt:

    ## valgrind
    find_program(CTEST_MEMORYCHECK_COMMAND NAMES valgrind)
    INCLUDE(Dart)
    

    generates the needed DartConfiguration.tcl whatever that is, and then re-invoking:

    ctest -T memcheck
    

    runs all tests also checking for memory errors.