Objective
All programs that use dynamic memory allocation (allocation) have the opportunity to encounter memory leakage problems, and there are three common tools in Linux to detect memory leaks, including:
- Mtrace
- Dmalloc
- Memwatch
1. Mtrace
Mtrace is the easiest to use among the three tools, Mtrace is a C function, declared and defined in <mcheck.h>, the function prototype is:
void mtrace (void);
In fact, Mtrace is similar to Malloc_hook malloc handler, but mtrace handler function has been written by the system for you, but in this case, the system how to know you want to write Malloc/free records where? To do this, first set the MALLOC_TRACE environment variable before calling Mtrace ():
#include <stdlib.h>
....
Setenv ("Malloc_trace", "Output_file_name", 1);
...
"output_file_name" is the name of the file where the test results are stored.
But the format of the test results is generally incomprehensible, and as long as there is the installation of mtrace, there will be a Perl script for Mtrace, in the shell input the following command:
mtrace [binary] Output_file_name
It transforms the contents of output_file_name into words that can be understood, such as "no memory leaks","0x12345678 Free is never alloc" and so forth.
For example, here is a function: (The principle of entry single exit is dropped)
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <mcheck.h>
int main () {
Char *hello;
Setenv ("Malloc_trace", "Output", 1);
Mtrace ();
if ((hello = (char *) malloc (sizeof (char))) = = = NULL) {
Perror ("Cannot allocate memory.");
return-1;
}
return 0;
}
After execution, the result is then output with mtrace:
-0x08049670 Free 3 was never alloc ' d 0X42029ACC
-0X080496F0 Free 4 was never alloc ' d 0x420dc9e9
-0x08049708 Free 5 was never alloc ' d 0x420dc9f1
-0x08049628 Free 6 was never alloc ' d 0x42113a22
-0x08049640 Free 7 was never alloc ' d 0x42113a52
-0x08049658 Free 8 was never alloc ' d 0x42113a96
Memory not freed:
-----------------
Address Size Caller
0x08049a90 0x1 at 0x80483fe
The last line indicates that a memory size of 1 bytes has not been released, presumably referring to the "hello" bar.
If we release the memory of this segment:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <mcheck.h>
int main () {
Char *hello;
Setenv ("Malloc_trace", "Output", 1);
Mtrace ();
if ((hello = (char *) malloc (sizeof (char))) = = = NULL) {
Perror ("Cannot allocate memory.");
return-1;
}
Free (hello);
return 0;
}
The results are as follows:
-0x080496b0 Free 4 was never alloc ' d 0X42029ACC
-0x08049730 Free 5 was never alloc ' d 0x420dc9e9
-0x08049748 Free 6 was never alloc ' d 0x420dc9f1
-0x08049668 Free 7 was never alloc ' d 0x42113a22
-0x08049680 Free 8 was never alloc ' d 0x42113a52
-0x08049698 Free 9 was never alloc ' d 0x42113a96
No memory leaks.
The principle of mtrace is to record each pair of malloc-free execution, if each malloc has the corresponding free, then there is no memory leakage, for any non-malloc/free situation occurred in the memory leak problem, mtrace can not find out.
Linux C Programming Memory leak Detection Tool (i): mtrace