C/C ++ Memory leakage detection tool in Windows/Linux

Source: Internet
Author: User

Http://hi.baidu.com/irenbest/blog/item/6353c7fdb3d77140d7887d13.html


Memory leakage detection in a window (taking the VC ++ environment as an example)

Flexibility and freedom are a major feature of the C language, but this feature also brings unavoidable side effects, such as memory leakage. As we all know, the problem of Memory leakage is complicated. When a program runs normally, you cannot see any exceptions, but it runs for a long time or when a specific operation is repeated multiple times under a specific condition, it is exposed. Therefore, memory leakage is often hard to discover and solve.

Visual Leak Detector (VLD) is a free memory leak detection tool for Visual C ++.
Lesser General Public License) protocol is open source, so it is safe to use VLD without worrying about copyright issues.

Use VLD

Download the VLD zip package from the website. The current maximum version is V1.0. decompress the package to obtain the VLD. h. vldapi. h. VLD. lib, vldmt. lib, vldmtdll. lib, dbghelp. DLL files, put all these. copy the H header file to the default include directory of VC, and copy all. copy the Lib file to the default lib directory of VC, and the installation is complete.

VLD is easy to use. You only need to include the VLD. h header file in the CPP or C file that contains the entry function. This include statement must be placed at the beginning. If the current project defines a pre-compiled head file (such as stdafx. h), put it in "# include
<Stdafx. h>. After the program is compiled normally and run in debug mode, check the output window of VC when the program is running.
Leak detector is now exiting. "A printed message. If the current program has no memory leakage before this condition information," no memory leaks detected. "Information printing, but if there is memory leakage, there will be similar information printing:

C: \ vctester21 \ sample \ vc6 \ samplemain. C (80): Main

Crt0.c (206): maincrtstartup

0x7c816fd7 (file and line number not available): registerwaitforinputidle

Data:

CD ................

Visual leak detector detected 1 memory leak.

This information indicates the function and source file number where the current Memory leakage occurs, and the address, length, and current memory value of the leaked memory block. Double-click the prompt message indicating the source code line. VC automatically jumps to the corresponding code line, so we can easily know which line has an error.

It can be seen that VLD is easy to use. If you are interested in its implementation principle, you can read the VLD source code or refer to dofty's article: using Visual
Leak detector detects Memory leakage.

2. Memory leakage detection in Linux (valgrind)

Valgrind is a tool used to debug memory problems when developing applications in Linux. It is especially good at discovering memory management problems. It can check the memory leakage during program running.

The above content is collected from the Internet

Its official website isHttp://www.valgrind.org/

Download the latest version of valgrind, Which is 3.2.0 currently. WgetHttp://www.valgrind.org/downloads/valkyrie-1.2.0.tar.bz2

Follow the instructions in readme to install the GNU software ,. /configure-make -- makeinstall. After installation, enter valgrind LS-L to verify whether the tool works properly (this is the method in README, in fact, it is used to verify the memory detection of the LS-l command. If you see a bunch of information, your tool can be used.

Initial use
Compile the following code: gcc-wall example. C-g-o example

#include <stdlib.h>void f(void){   int* x = malloc(10 * sizeof(int));   x[10] = 0;        // problem 1: heap block overrun}                    // problem 2: memory leak -- x not freedint main(void){     f();     return 0;}

Note: The-G option of GCC enables valgrind to indicate the line number of the Code with the corresponding information when debugging the output.

Valgrind -- tool = memcheck -- leak-check = yes./Example
= 6742 = memcheck, a memory error detector for x86-linux.
= 6742 = copyright (c) 2002-2004, and gnu gpl 'd, by Julian Seward et al.
= 6742 = Using valgrind-2.2.0, a program supervision framework for x86-linux.
= 6742 = copyright (c) 2000-2004, and gnu gpl 'd, by Julian Seward et al.
= 6742 = For more details, Rerun with:-V
= 6742 =
==6742 = Invalid write of size 4
= 6742 = at 0x8048384: F (example. C: 6)
= 6742 = by 0x80483ac: Main (example. C: 12)
= 6742 = address 0x1b908050 is 0 bytes after a block of size 40 alloc 'd
= 6742 = at 0x1b904984: malloc (vg_replace_malloc.c: 131)
= 6742 = by 0x8048377: F (example. C: 5)
= 6742 = by 0x80483ac: Main (example. C: 12)
= 6742 =
= 6742 = Error Summary: 1 errors from 1 contexts (suppressed: 12 from 1)
= 6742 = malloc/free: in use at Exit: 40 bytes in 1 blocks.
= 6742 = malloc/free: 1 allocs, 0 frees, 40 bytes allocated.
= 6742 = for counts of detected errors, Rerun with:-V
= 6742 = searching for pointers to 1 not-freed blocks.
= 6742 = checked 1360800 bytes.
= 6742 =
= 6742 =
==6742 = 40 bytes in 1 blocks are definitely lost in loss record 1 of 1
= 6742 = at 0x1b904984: malloc (vg_replace_malloc.c: 131)
= 6742 = by 0x8048377: F (example. C: 5)
= 6742 = by 0x80483ac: Main (example. C: 12)
= 6742 =
= 6742 = leak summary:
= 6742 = definitely lost: 40 bytes in 1 blocks.
==6742 = possibly lost: 0 bytes in 0 blocks.
= 6742 = still reachable: 0 bytes in 0 blocks.
= 6742 = suppressed: 0 bytes in 0 blocks.
= 6742 = reachable blocks (those to which a pointer was found) are not shown.
= 6742 = to see them, Rerun with: -- show-reachable = Yes

The above C program has two errors: 1. the array subscript is out of bounds; 2. the allocated memory is not released, causing memory leakage. For Error 1, check the debugging information snippet of valgrind.

==6742 = Invalid write of size 4
= 6742 = at 0x8048384: F (example. C: 6)
= 6742 = by 0x80483ac: Main (example. C: 12)
= 6742 = address 0x1b908050 is 0 bytes after a block of size 40 alloc 'd
= 6742 = at 0x1b904984: malloc (vg_replace_malloc.c: 131)
= 6742 = by 0x8048377: F (example. C: 5)

For Error 2, check this

= 6742 = malloc/free: 1 allocs, 0 frees, 40 bytes allocated.

......

==6742 = 40 bytes in 1 blocks are definitely lost in loss record 1 of 1
= 6742 = at 0x1b904984: malloc (vg_replace_malloc.c: 131)
= 6742 = by 0x8048377: F (example. C: 5)
= 6742 = by 0x80483ac: Main (example. C: 12)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.