Is anyone aware of extensions to CppUnit that can be used to make assertions on a test by test basis concerning memory leaks.
i.e. CPPUNIT_ASSERT_NO_LEAKS()?
Essentially, I want to be able to fail specific tests when the execution of the test results in leaked memory.
Memory profilers are tools that can monitor memory usage and help detect memory leaks in an application. Profilers can also help with analyzing how resources are allocated within an application, for example how much memory and CPU time is being used by each method.
The best approach to checking for the existence of a memory leak in your application is by looking at your RAM usage and investigating the total amount of memory been used versus the total amount available. Evidently, it is advisable to obtain snapshots of your memory's heap dump while in a production environment.
Memory leak detection tools. A number of tools help in the hunt for memory leaks. The most popular free ones are dmalloc and mpatrol. These tools provide debugging versions of the heap that record and check all allocations to facilitate analysis of leaks and dangling pointers.
If you're running on Linux, you could run your tests with memcheck.
The Client Requests section of the manual describes several useful macros, of which one is noted as being useful for testing:
VALGRIND_COUNT_LEAKS
: fills in the four arguments with the number of bytes of memory found by the previous leak check to be leaked, dubious, reachable and suppressed. Again, useful in test harness code, after callingVALGRIND_DO_LEAK_CHECK
.
The macro is defined in memcheck.h
(likely in /usr/include/valgrind
), and the sequence you want will resemble
unsigned long base_definite, base_dubious, base_reachable, base_suppressed;
VALGRIND_DO_LEAK_CHECK;
VALGRIND_COUNT_LEAKS(base_definite, base_dubious, base_reachable, base_suppressed);
// maybe assert that they're zero!
// call test
unsigned long leaked, dubious, reachable, suppressed;
VALGRIND_DO_LEAK_CHECK;
VALGRIND_COUNT_LEAKS(leaked, dubious, reachable, suppressed);
CPPUNIT_ASSERT_EQUAL(base_leaked, leaked);
// etc.
Repeating that for every test would be a pain, so you might write macros of your own or, even better, a specialized TestRunner.
CPPUNIT doesn't have memory leak checks support by default.
The project has been recontinued now (it was stopped for a long time) and this may be a feature of CPPUNIT2, you can propose (or write) to the authors.
If you're looking for a Unit test framework with memory leak detection support you can try looking at CppUTest. It is the project used by Martin Fowler and Bob Martin on some TDD courses. It is pretty good.
On Windows it would be a pretty simple matter of using some calls to the debug heap to get CppUnit to act on this information using _CrtMemCheckpoint()
and _CrtMemDifference()
:
There are drawbacks:
Where I work we build our unit tests with purify. Then our continuous integration platform pulls both the number of testcases that succeeded/failed and the number of leaked bytes (+ lint and coverity results) and shows it on a web page. I can highly recommend doing it this way.
Sorry for not providing the solution you wanted.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With