dimanche 15 février 2015

Avoid memory leak with nested suites in CuTest

I recently used Valgrind to analyze my C project using unit tests written using the CuTest library. As it turned out, all my CuSuites and CuTests leaked. I am using a master-suite setup where lots of smaller suites are added together into one big suite which I then run.


First I tried to use CuSuiteDelete on the master suite, but all the other suites still leaked. In my next attempt I deleted all the other suites first, before the master one. This caused a segfault, because deleting a suite also frees all tests within it. Since adding a suite to another suite simply copies all the CuTest pointers, the second call to CuSuiteDelete will try to free already freed CuTests.


Is there another way to do this, avoiding both memory leaks and segfault? If not, this is a bug in CuTest. Although unit tests are usually the only thing being run by a process before exiting, this is still undocumented.


Below are some solutions I came up with:



  1. Add refcount to every CuTest. CuSuiteAddSuite would increment this and CuTestDelete would decrement it, removing the test only if refcount reaches 0.

  2. Add a CuSuite array to every suite where CuSuiteAddSuite would put added suites. This would probably require a refcount for suites instead.


Aucun commentaire:

Enregistrer un commentaire