Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ - Is it possible to implement memory leak testing in a unit test?

I'm trying to implement unit testing for my code and I'm having a hard time doing it.

Ideally I would like to test some classes not only for good functionality but also for proper memory allocation/deallocation. I wonder if this check can be done using a unit testing framework. I am using Visual Assert btw. I would love to see some sample code , if possible !

like image 704
sevaxx Avatar asked Jun 05 '10 15:06

sevaxx


People also ask

What is memory leak in testing?

In computer science, a memory leak is a type of resource leak that occurs when a computer program incorrectly manages memory allocations in a way that memory which is no longer needed is not released. A memory leak may also happen when an object is stored in memory but cannot be accessed by the running code.

Which tool is used to detect memory leak in testing?

Using Memory Profilers 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.

What is memory leak why it should be avoided in C?

Master C and Embedded C Programming- Learn as you go The memory leak occurs, when a piece of memory which was previously allocated by the programmer. Then it is not deallocated properly by programmer. That memory is no longer in use by the program. So that place is reserved for no reason.

Do I need unit tests to check memory usage?

You don't need unit tests you need memory profiler. You can start with CLR Profiler. dotMemory Unit framework has capabilities to programmatically check amount of certain objects allocated, memory traffic, make and compare memory snapshots.

What is memory leak in C++?

free () operator is used in C to deallocate dynamic memory. delete operator is used in C++ to deallocate dynamic memory. So, finally to summerize it, Memory Leak is improper use of dynamic memory or the heap section of the memory that causes the memory consumption of our program to increase over a period of time.

Why is memory testing important for embedded systems?

The need for memory testing is most apparent during product development, when the reliability of the hardware and its design are still unproven. However, memory is one of the most critical resources in any embedded system, so it may also be desirable to include a memory test in the final release of your software.

Why is it so hard to write a unit test?

However, it can sometimes be quite difficult to write a good unit test for a particular piece of code. Having difficulty testing their own or someone else’s code, developers often think that their struggles are caused by a lack of some fundamental testing knowledge or secret unit testing techniques.


2 Answers

You can use the debug functionality right into dev studio to perform leak checking - as long as your unit tests' run using the debug c-runtime.

A simple example would look something like this:

#include <crtdbg.h>
struct CrtCheckMemory
{
  _CrtMemState state1;
  _CrtMemState state2;
  _CrtMemState state3;
  CrtCheckMemory()
  {
    _CrtMemCheckpoint(&state1);
  }
  ~CrtCheckMemory()
  {
    _CrtMemCheckpoint(&state2);
    // using google test you can just do this.
    EXPECT_EQ(0,_CrtMemDifference( &state3, &state1, &state2));
    // else just do this to dump the leaked blocks to stdout.
    if( _CrtMemDifference( &state3, &state1, &state2) )
      _CrtMemDumpStatistics( &state3 );
  }
};

And to use it in a unit test:

UNIT_TEST(blah)
{
  CrtCheckMemory check;

  // TODO: add the unit test here

}

Some unit test frameworks make their own allocations - Google's for example allocates blocks when a unit test fails, so any test block that has a fail for any other reason always also has a false positive "leak".

like image 85
Chris Becke Avatar answered Sep 30 '22 05:09

Chris Becke


You can use Google's tcmalloc allocation library, which provides a heapchecker.

(Note that heapchecking may add noticeable overhead to your program's performance, so you probably only want to enable it on debug builds or unit tests.)

And you asked for example code, so here it is.

like image 27
Stephen Avatar answered Sep 30 '22 04:09

Stephen