Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using CppUnit for memory leak detection

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.

like image 217
Jeremy Mayhew Avatar asked Jun 16 '09 13:06

Jeremy Mayhew


People also ask

What is the best tool to detect memory leaks?

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.

How do you detect memory leaks in performance testing?

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.

How do you find memory leaks in embedded systems?

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.


4 Answers

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 calling VALGRIND_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.

like image 166
Greg Bacon Avatar answered Nov 15 '22 00:11

Greg Bacon


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.

like image 42
Edison Gustavo Muenz Avatar answered Nov 15 '22 00:11

Edison Gustavo Muenz


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():

  • http://msdn.microsoft.com/en-us/library/wc28wkas.aspx

There are drawbacks:

  • you'd have to place something manually at the start of the test to get the checkpoint (maybe there's a way to integrate that into CppUnit somehow)
  • it's Windows only (there's probably something similar on the various other platforms)
  • it'll only work for builds with the Debug CRT
like image 35
Michael Burr Avatar answered Nov 14 '22 22:11

Michael Burr


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.

like image 24
tombjo Avatar answered Nov 14 '22 23:11

tombjo