C - Use valgrind to detect memory leaks and context error in my criterion unit tests

242 views Asked by At

I'm trying to write unit tests for a C lib I've made.
To do so I'm using the lib criterion.

I would like to use valgrind to detect memory leaks and context error, when I run make test it generate a unit_test.o file that I can run throw valgrind and here is the result :

test: clean $(TEST_OBJ)
    $(CC) -o ${TEST_NAME} ${SRC} $(TEST_SRC) --coverage -lcriterion
    ./${TEST_NAME}

enter image description here

As you can see, I have 0 leaks and 0 context error which is not true as the tested code contains both. If I run the same code in the main function It will detect both leaks and contexts.

It looks like criterion execute the unit_tests in a kind of "safe" context and free all the memory itself.
My question is how can I test leaks and contexts from my criterion unit tests ? Or, what are the alternative to test it ?

1

There are 1 answers

2
Jonathan Leffler On

You'll have to look at the documentation for Criterion — but the chances are high that the default mode launches tests in separate processes.

[…Time passeth…] The source code for the Snaipe/Criterion unit test framework is available on GitHub (at https://github.com/Snaipe/Criterion), and one of the features claimed on the home page is "Test [sic] are isolated in their own process, crashes and signals can be reported and tested".

However, Valgrind has options to help, notably:

  • --trace-children=no|yes Valgrind-ise child processes (follow execve)? [no].
  • --trace-children-skip=patt1,patt2,... specifies a list of executables that --trace-children=yes should not trace into.
  • --trace-children-skip-by-arg=patt1,patt2,... same as --trace-children-skip= but check the argv[] entries for children, rather than the exe name, to make a follow/no-follow decision.

Setting --trace-children=yes as an option to Valgrind should help you.