GCC (Ubuntu 4.8.4-2ubuntu1~14.04) 4.8.4
Copyright (C) Free Software Foundation, Inc.
This was free software; See the source for copying conditions. There is NO
Warranty Not even to merchantability or FITNESS for A particular PURPOSE.
First, Mtrace
1. Introduction
Mtrace is a memory debugging tool included in the GNU C library. The Mtrace () function installs hook functions for memory allocation functions (Malloc,realloc,free), which are used to record trace information about the allocation and release of memory, which can be used to discover memory leaks.
The Muntrace () function invalidates the installed hook function, so the allocation of memory is no longer tracked. When Mtrace () is called, it checks the value of the environment variable malloc_trace, and the memory allocation information is recorded in the file that the environment variable refers to.
Typically, the Mtrace () program is called before execution, and muntrace is not called because some memory is released at the end of the program. The trace information generated by mtrace is a text file, but is not easy to understand and can be explained by Perl scripts provided by the GNU C Library. In order to get a memory leak in the exact location of the file, the program needs to be compiled in debug mode .
2. Steps to use
1) The main function is located in the file containing the header file of the <mcheck.h>,main function at the beginning of the call Mtrace ()
2) compiling the source code in debug mode
3) Setting the environment variable malloc_trace=output_file specifies the file path of the recording trace information (the actual test indicates that the environment variable is valid in the code)
4) Execute executable program./test, generate Output_file file
5) Execute the Perl script provided by GNU C: mtrace test output_file
3. Example
TEST.C:
1#include <mcheck.h>2#include <stdlib.h>3#include <stdio.h>4 5 intMainintargcChar*argv[])6 { 7Setenv"Malloc_trace","output_file",1); 8mtrace (); 9 Ten for(intj =0; J <2; J + +) One malloc(2);/*never freed--a memory leak*/ A - calloc(2,2);/*never freed--a memory leak*/ -exit (exit_success); the}
Execution: gcc-g test.c-o test
./test
Mtrace Test Output_file
Test results:
-0x084f8008 Free 5 was never alloc ' d 0xb7617024
-0x084f8070 Free 6 was never alloc ' d 0xb76cb866
-0x084f8090 Free 7 was never alloc ' d 0xb76cb86e
Memory not freed:
-----------------
Address Size Caller
0x084f8418 0x2 at/home/zpy/tmp/test.c:10 (discriminator 2)
0x084f8428 0x2 at/home/zpy/tmp/test.c:10 (discriminator 2)
0x084f8438 0x4 at/home/zpy/tmp/test.c:14
The size and location of the memory leaks are displayed.
4. Summary:
Mtrace only detects memory allocated by malloc, realloc, and so on in C, and cannot allocate memory through new in C + +, it needs to recompile the source code to detect it.
Second, Memwatch
1. Introduction
Memwatch is a memory leak detection tool with the following characteristics:
-
- Supports ANSI C
- Detects multiple releases of memory, and how errors are released
- Detecting non-freed memory
- Detecting overflow and underflow in memory buffer
- Detects the write to the wild pointer
- Partially supports C + + (default disabled)
2. How to use:
1) Download Memwatch package, unzip
2) include the MEMWATH.H header file in all code source files that need to be instrumented
3) Recompile the source code and specify the macro definition Memwatch Memwatch_stdio
4) Execute the program./test
5) View the generated file Memwatch.log content
3. Example:
TEST.c
1#include <stdlib.h>2#include <stdio.h>3#include"memwatch.h" 4 intMainintargcChar*argv[])5 { 6 for(intj =0; J <2; J + +) 7 malloc(2);/*never freed--a memory leak*/ 8 9 calloc(2,2);/*never freed--a memory leak*/ Tenexit (exit_success); One}
Compilation: Gcc-dmemwatch-dmw_stdio test.c memwatch.c-o Test
Run:./test
View Memwatch.log File:
1 2============= Memwatch2.71Copyright (C)1992-1999Johan Lindh =============3 4Started at Sat APR - Geneva: Wu: - . 5 6Modes: __stdc__ --bit mwdword== (unsignedLong) 7mwroundalloc==8 sizeof(mwdata) = = +mwdatasize== + 8 9 TenStopped at Sat APR - Geneva: Wu: - . One AUnfreed: <3> test.c (9),4Bytes at0x92a6260{xx xx xx xx .. .. .. .. .. .. .. .. .. .. .. .. ....} -Unfreed: <2> test.c (7),2Bytes at0x92a6228{fe fe ........... .........} -Unfreed: <1> test.c (7),2Bytes at0x92a61f0{fe fe ........... .........} the -Memory Usage statistics (Global): -N) Umber of allocations made:3 -L) argest Memory usage:8 +T) Otal of All Alloc () calls:8 -U) nfreed bytes Totals:8
4. Summary:
Memwatch is well able to detect memory leaks in C, and the memory allocated in C + + is not recommended for tool detection because it does not support C + +. Use the tool to recompile the source code and need to include the header file "Memwatch.h" in all the source files
Third, Leaktracer
1. Introduction
Leaktracer is a small C + + memory leak Detection tool. It can detect memory problems such as:
-
- Non-freed memory
- Overwritten outside the allocated memory range
- Attempt to free unallocated memory (such as releasing garbage pointer and multiple releases)
- Releases a mismatch with the allocation function, such as using the new[] allocation, while using Delete to release
When using Leaktracer, run your program through the provided Leakcheck script, which uses ld_preload to "rewrite" the upper layer of your function. If your platform does not support ld_preload, you will need to add the LEAKTRACER.O object file to the makefile file and then run your application
Leaktracer uses GDB to output the location where a memory leak occurs, which is detected by the override operator new and operator delete.
2. How to use:
1) Download Leaktracer
2) Extract, execute make, generate leaktracer.so
3) Compiling the source code in debug mode
4) Run the program via the provided Leakcheck:./leakcheck./test, generates Leak.out file
5) analyze results by providing Leak-analyze script:./leak-analyze test Leak.out (leak-analyze loads leaktracer.so, so note the path)
3. Example:
1 //Small Leaky test program 2 3 voidfoo () {4 int*x =New int; 5 } 6 7 intMain () {8 int*z =New int[Ten]; 9 Char*q =New Char[4]; Tenq[4] ='x';//MAGIC Overrun One //commenting out should make this abort A //Delete q; -foo (); -foo (); the DeleteZ; - DeleteZ//Delete value twice -}
Perform:
g++-G test.cc-o test
./leakcheck./test
./leak-analyze Test Leak.out
Test results:
Gathered4(3unique) points of data. Reading symbols fromTest...done. (GDB) #--alloc:different allocation Schemesalloc here:0x8048569 is inchMain () (test.cc:8).7 intMain () {8 int*z =New int[Ten];.. FreeHere:0x804859d is inchMain () (test.cc: -). - DeleteZ//Delete value twice#--leak:counted 2x/total Size:80x804854f is inchFoo () (test.cc:4).3 voidfoo () {4 int*x =New int;#--leak:counted 1x/total Size:40x8048579 is inchMain () (test.cc:9).8 int*z =New int[Ten];9 Char*q =New Char[4];#--DeleteOn not allocated memory:counted 1x0x80485a9 is inchMain () (test.cc: -). - DeleteZ//Delete value twice -}
The test results reflect the code associated with the memory leak, along with the file and line number, which is very nice!
4. Summary:
Leaktracer can only detect new/new[] allocated memory and cannot be detected for allocated memory such as malloc in C. There is no need to recompile the source code.
C + + memory leak detection