Mtrace

is the memory debugger included in the GNU C Library.

Use
Note that  tool works only with single threaded applications. One thread could temporarily remove the hook while another thread could malloc memory at the sametime leading to missed allocations in a multithreaded application!

The function  installs handlers for ,   and  ; the function   disables these handlers. Their prototypes, defined in the header file, are

void ;

void ;

The handlers log all memory allocations and frees to a file defined by the environment variable MALLOC_TRACE (if the variable is unset, describes an invalid filename, or describes a filename the user does not have permissions to, the handlers are not installed).

A perl script called, not to be confused with the function of the same name, is also distributed with the GNU C Library; the script parses through the output file and reports all allocations that were not freed.

Bad source code
The following is an example of bad source code. The problem with the program is that it allocates memory, but doesn't free the memory before exiting.

MTrace usage

 * 1) Set the environment variable   to the pathname of the desired output file.  Setting environment variables is slightly different in each shell.  In Bourne Shell-compatible shells, like Bash, the command is as follows:
 * 2) Include   in the source code.  This is done, for example, by adding the following line to the top of a C or C++ file, as shown below:
 * 3) Call the function   before you start allocating memory.  It is usually easiest to call   at the very beginning of the   function:
 * To delineate the end of the code that should be traced, call the function .  This is usually done at the end of the   function:
 * 1) Compile and run the program as usual.  Note that you need to compile with the   option to get useful output.  In GCC on Linux, this can be done using the following commands for a C program:
 * 2) Memory leak information will be reported in the file specified by the   environment variable.  The difficulty is, this file will be in a computer-readable format.  Most Linux machines come with a console command called , that converts the computer readable format into human-readable text as shown below.  If you do not have access to this console command, there is a Perl script, of the same name, that can be downloaded to accomplish the same task.  The   syntax is as follows:
 * For example:
 * 1)   can be used with parallel computing but one process at a time, using a condition on the rank like:
 * 1) Compile and run the program as usual.  Note that you need to compile with the   option to get useful output.  In GCC on Linux, this can be done using the following commands for a C program:
 * 2) Memory leak information will be reported in the file specified by the   environment variable.  The difficulty is, this file will be in a computer-readable format.  Most Linux machines come with a console command called , that converts the computer readable format into human-readable text as shown below.  If you do not have access to this console command, there is a Perl script, of the same name, that can be downloaded to accomplish the same task.  The   syntax is as follows:
 * For example:
 * 1)   can be used with parallel computing but one process at a time, using a condition on the rank like:
 * For example:
 * 1)   can be used with parallel computing but one process at a time, using a condition on the rank like:
 * 1)   can be used with parallel computing but one process at a time, using a condition on the rank like:

MTrace output
If the mtrace command reports “No Memory Leaks”, then all memory that was allocated in the last execution of that program was also released, which is the way it should be. If, on the other hand, mtrace gives output such as that below, it means the programmer still has some work to do. Memory not freed: -  Address     Size     Caller 0x08049910     0x4  at /home/sureshsathiah/tips/leak.c:9

Good source code
The following is an example of good source code. It releases memory after it is allocated, and it uses mtrace to notify the programmer if there are memory leaks.