Memory leak

Description
A memory leak is an unintentional form of memory consumption whereby the developer fails to free an allocated block of memory when no longer needed. The consequences of such an issue depend on the application itself. Consider the following general three cases:

Examples
The following example is a basic memory leak in C:

int main(int argc, char **argv) {     int count = 0; char *pointer = NULL; for(count=0; count
 * 2) include 
 * 1) define LOOPS    10
 * 2) define MAXSIZE  256


 * In this example, we have 10 allocations of size MAXSIZE. Every allocation, with the exception of the last, is lost. If no pointer is pointed to the allocated block, it is unrecoverable during program execution. A simple fix to this trivial example is to place the free call inside of the 'for' loop.

Related Countermeasures
Avoiding memory leaks in applications is difficult for even the most skilled developers. Luckily, there are tools with aide in tracking down suck memory leaks. One such example on the Unix/Linux environment is Valgrind. Valgrind runs the desired program in an environment such that all memory allocation and de-allocation routines are checked. At the end of program execution, Valgrind will display the results in an easy to read manner. The following is the output of Valgrind using the flawed code above:

[root@localhost Programming]# gcc -o leak leak.c [root@localhost Programming]# valgrind ./leak ==6518== Memcheck, a memory error detector for x86-linux. ==6518== Copyright (C) 2002-2005, and GNU GPL'd, by Julian Seward et al. ==6518== Using valgrind-2.4.0, a program supervision framework for x86-linux. ==6518== Copyright (C) 2000-2005, and GNU GPL'd, by Julian Seward et al. ==6518== For more details, rerun with: -v ==6518== ==6518== ==6518== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 13 from 1) ==6518== malloc/free: in use at exit: 2304 bytes in 9 blocks. ==6518== malloc/free: 10 allocs, 1 frees, 2560 bytes allocated. ==6518== For counts of detected errors, rerun with: -v ==6518== searching for pointers to 9 not-freed blocks. ==6518== checked 49152 bytes. ==6518== ==6518== LEAK SUMMARY: ==6518==   definitely lost: 2304 bytes in 9 blocks. ==6518==     possibly lost: 0 bytes in 0 blocks. ==6518==   still reachable: 0 bytes in 0 blocks. ==6518==        suppressed: 0 bytes in 0 blocks. ==6518== Use --leak-check=full to see details of leaked memory.


 * As we can see in this example, we leak 9 block with a total of 2304 bytes as we expected. If we were to place the free call inside of the loop, we would get 0 memory blocks definitely lost.