White Box Testing
Test your code
   Home      Memory leaks in C,C++
 

Types of Memory leaks in C,C++

1) Programmer forgets to explicitly release(free or delete) the dynamically allocated memory ( malloc,calloc,realloc or new) by mistake. Its simplest form of memory leak
 
               Mem *p;
               ...
               ...
               p = new Mem();
               //delete p;
 
 
2) Allocated memory block is not deallocated fully.
 
               Mem p[];
               ...
               ...
               p = new Mem[10];
               delete p;
 
As in this example memory is allocated for 10 arrays block while de-allocated for single block causing resource leak. Always call right form of deallocation function (for ex in c++ if you call new [] then delete [] must be called)
 
 
3) The address of dynamically allocated memory has not been persevered for later de-allocation.
 
               Mem *p,*q;
               ...
               ...
               p = new Mem();
               q = new Mem();
               q = p;
               delete p;
               delete q;     // will not free the pointer to the original allocated memory.
 
 
4) Some standard or user defined function allocates memory dynamically in that case explicitly deallocation requires. For ex. Standard function strdup(). It duplicates a string and returns a pointer to it. The duplicated string is created on the heap. If we do not free it explicitly, it causes mem leak.
 
               #include <string.h>
               ...
               char *oldString = “Old String”;
               char *newStrig = strdup(oldString)
               ...
               //free(newString);       // If we do not free newString explicitly, it causes memory
 
 
5) In case of Inheritance wrong delete operation performed.
 
               BaseClass *ptr = new DerivedClass;
               ...
               delete ptr;  // call the destructor ~ BaseClass()
 
In this case resource leak is due to destructor called for BaseClass while base pointer containing derived object. Use virtual destructor to avoid these types of resource leaks.
 
 
6) When assigning new values to all the various members of an object, if any member is a dynamically allocated segment then it must be explicitly deallocated prior to assigning a new value.
 
               Mem *ptr = new Mem;
               ...
               ...
               delete ptr;
 
 
7) The problem of a program module obtaining a memory segment without realizing that it is now the owner and therefore responsible for its deallocation. For example an object created by some portion/function ‘ABC’  of the program provided to another function ’XYZ’ . The object is dynamically created by ABC but not having ownership and function XYZ is not aware of the ownership issue.
 
 
 

Tracing Memory Leaks

Now a day’s program size going bigger. It’s tedious to keep track of all resource allocation. There are a lot of ways to track the resource leakage.The commonly method used for memory leak now a days are as debug library, memory  tools e.t.c.
 
There are a lot of debug library (some free source code) available. Debug library implements wrappers for the normal heap allocators. Build your application on base of these debug library in place of default heap allocators. As these library keep track of all memory allocated on heap will report the entire resource leak. This is not necessary the debug library you have chosen will work on all machines
 
Memory tools are commonly used these days. Memory analysis can be done through statically and dynamically. In static memory checkers tool the resource leak test done at the time of code compilation. There are certain limitations on static memory checkers tool. These tools generally work on static code analysis. They generally focus on syntax of code. Dynamic memory checkers tool work by executing the code.
 
You can also implement your own debug library or memory checkers tool by probing the bellow points.
1. Allocations: Track for each allocation done and record the memory consumed by allocation.
 
2. Deallocations: Track for each deallocation and see the record of memory consumed.
 
3. Locals values: Track for memory consumed by local variable and formal parameters.
 
4. Assigned values: Track for the initialization and assignments of objects.
 
If you will find any resource not freed by source code report as leaked