Linux/C + +on common memory leak detection tools areValgrind, Rational purify. Valgrindfree. Valgrindcan be in +bit or -bitPowerpc/linuxwork on the kernel.
Valgrind The toolkit contains several tools, such asMemcheck,cachegrind,helgrind, Callgrind,Massif. Here are some of the tools that you'll work with:
Memcheck the tool mainly checks for the following program errors:
? use of uninitialized memory(use of uninitialised memory)
? using memory that has already been freed(reading/writing memory after it had been free'd)
? use more thanmallocAllocated Memory Space(reading/writing off the end of malloc'd blocks)
? illegal access to the stack(reading/writing inappropriate areas on the stack)
? is there a free space for the application ?(Memory leaks–where pointers to malloc'd blocks is lost forever)
?Malloc/free/new/deleteapplication and release memory matching(mismatched use of malloc/new/new [] vs Free/delete/delete [])
?srcand theDSTthe overlap(overlapping src and DST pointers in memcpy () and related functions)
Valgrind the use of statically allocated arrays is not checked.
Valgrind takes up more memory--up to twice times the normal usage of your program. If you useValgrindto detect a problem with a program that uses a lot of memory, it may take a long time to run the test
2.1. Download Installation
http://www.valgrind.org
installation
./configure;make;make Install
2.2. Compiling the program
The detected program is added –G-fno-inlineThe compile option retains debugging information.
2.3. Memory Leak Detection
$ valgrind--tool=memcheck--log-file=/home/trunk/valgrind_log_all--leak-check=full--error-limit=no-- Show-leak-kinds=all/opt/lim/bin/limserver
which --leak-check=full refers to a full check of memory leaks, --show-reachable=yes is where the memory leaks are displayed, --trace-children=yes is to follow the child process. When the program exits normally, valgrind will naturally output the information of memory leaks.
1. Memory leaks:
#include <stdio.h>void function () {int *p = (int*) malloc (10*sizeof (int)); P[10] = 0;} int main () {function (); return 0;}
Related logs:
==20220== Memcheck, a memory error detector
==20220== Copyright (C) 2002-2010, and GNU GPL ' d, by Julian Seward et al.
==20220== Using Valgrind-3.6.1-debian and Libvex; Rerun with-h for copyright info
==20220== Command:./test
==20220== Parent pid:20160
==20220==
==20220== Invalid Write of size 4
==20220== at 0x80483ff:function (in/mnt/documents/training/valgrind/test)
==20220== by 0x8048411:main (in/mnt/documents/training/valgrind/test)
==20220== Address 0x41be050 is 0 bytes after a block of size alloc ' d
==20220== at 0x4028876:malloc (vg_replace_malloc.c:236)
==20220== by 0x80483f5:function (in/mnt/documents/training/valgrind/test)
==20220== by 0x8048411:main (in/mnt/documents/training/valgrind/test)
==20220==
==20220==
==20220== HEAP SUMMARY:
==20220== in with exit:40 bytes in 1 blocks
==20220== Total heap Usage:1 allocs, 0 frees, Bytes allocated
==20220==
==20220== LEAK SUMMARY:
==20220== definitely lost:40 bytes in 1 blocks
==20220== indirectly lost:0 bytes in 0 blocks
==20220== possibly lost:0 bytes in 0 blocks
==20220== still reachable:0 bytes in 0 blocks
==20220== suppressed:0 bytes in 0 blocks
==20220== rerun with--leak-check=full to see details of leaked memory
==20220==
==20220== for counts of detected and suppressed errors, rerun with:-V
==20220== ERROR summary:1 errors from 1 contexts (suppressed:11 from 6)
2. Using uninitialized memory
#include <stdio.h>int main () {int A; if (a==1) {printf ("a==%d\n", a); } return 0;}
Log analysis:
==20345== Memcheck, a memory error detector
==20345== Copyright (C) 2002-2010, and GNU GPL ' d, by Julian Seward et al.
==20345== Using Valgrind-3.6.1-debian and Libvex; Rerun with-h for copyright info
==20345== Command:./test
==20345==
==20345== Conditional Jump or move depends on uninitialised value (s)
==20345== at 0x80483f2:main (in/mnt/documents/training/valgrind/test)
==20345==
==20345==
==20345== HEAP SUMMARY:
==20345== in with exit:0 bytes in 0 blocks
==20345== Total heap usage:0 allocs, 0 frees, 0 bytes allocated
==20345==
==20345== all heap blocks were freed--no leaks is possible
==20345==
==20345== for counts of detected and suppressed errors, rerun with:-V
==20345== use--track-origins=yes to see where uninitialised values come from
==20345== ERROR summary:1 errors from 1 contexts (suppressed:11 from 6)
You can use--track-origins=yes to get more information.
3. Memory read/write out of bounds
#include <stdio.h>int main () {int *a = (int*) malloc (5*sizeof (int)); A[5] = 1; return 0;}
==20368== Memcheck, a memory error detector
==20368== Copyright (C) 2002-2010, and GNU GPL ' d, by Julian Seward et al.
==20368== Using Valgrind-3.6.1-debian and Libvex; Rerun with-h for copyright info
==20368== Command:./test
==20368==
==20368== Invalid Write of size 4
==20368== at 0x8048404:main (in/mnt/documents/training/valgrind/test)
==20368== Address 0x41be03c is 0 bytes after a block of size alloc ' d
==20368== at 0x4028876:malloc (vg_replace_malloc.c:236)
==20368== by 0x80483f8:main (in/mnt/documents/training/valgrind/test)
==20368==
==20368==
==20368== HEAP SUMMARY:
==20368== in with exit:20 bytes in 1 blocks
==20368== Total heap Usage:1 allocs, 0 frees, Bytes allocated
==20368==
==20368== LEAK SUMMARY:
==20368== definitely lost:20 bytes in 1 blocks
==20368== indirectly lost:0 bytes in 0 blocks
==20368== possibly lost:0 bytes in 0 blocks
==20368== still reachable:0 bytes in 0 blocks
==20368== suppressed:0 bytes in 0 blocks
==20368== rerun with--leak-check=full to see details of leaked memory
==20368==
==20368== for counts of detected and suppressed errors, rerun with:-V
==20368== ERROR summary:1 errors from 1 contexts (suppressed:11 from 6)
4. Memory Request Release Management error
#include <stdio.h>int main () {int *a = new Int[5]; /*free (a); */delete A; return 0;}
==20387== Memcheck, a memory error detector
==20387== Copyright (C) 2002-2010, and GNU GPL ' d, by Julian Seward et al.
==20387== Using Valgrind-3.6.1-debian and Libvex; Rerun with-h for copyright info
==20387== Command:./test
==20387==
==20387== mismatched free ()/Delete/delete []
==20387== at 0x4027919:operator Delete (void*) (vg_replace_malloc.c:387)
==20387== by 0x8048498:main (in/mnt/documents/training/valgrind/test)
==20387== Address 0x42f2028 is 0 bytes inside a block of size alloc ' d
==20387== at 0x4027f65:operator new[] (unsigned int) (vg_replace_malloc.c:299)
==20387== by 0x8048488:main (in/mnt/documents/training/valgrind/test)
==20387==
==20387==
==20387== HEAP SUMMARY:
==20387== in with exit:0 bytes in 0 blocks
==20387== Total Heap Usage:1 Allocs, 1 frees, Bytes allocated
==20387==
==20387== all heap blocks were freed--no leaks is possible
==20387==
==20387== for counts of detected and suppressed errors, rerun with:-V
==20387== ERROR summary:1 errors from 1 contexts (suppressed:17 from 6)
Linux C memory Leak detection Tool Valgrind