Linux/C + + memory leak Detection Tool: Valgrind

Source: Internet
Author: User
Tags valgrind

Valgrind is a memory debugging tool for Linux (x86, x86_64, and PPC32) programs that can monitor memory usage of the compiled binaries (malloc and free in C, and new and delete in the C + +). Identify memory leak issues.

The Memcheck tool included in Valgrind can check for the following program errors:

Using uninitialized memory (use of uninitialised memories)
Use memory that has been freed (reading/writing memories after it have been free ' d)
Use over malloc allocated memory space (reading/writing off the end of malloc ' d blocks)
Illegal access to stacks (reading/writing inappropriate areas on the stack)
Whether the requested space has been released (Memory leaks-where pointers to malloc ' d blocks is lost forever)
Malloc/free/new/delete application and Release memory matching (mismatched use of malloc/new/new [] vs Free/delete/delete [])
SRC and DST overlap (overlapping src and DST pointers in memcpy () and related functions)
Repeat free

1. Compile and install Valgrind:

wget http://valgrind.org/downloads/valgrind-3.4.1.tar.bz2
Tar xvf valgrind-3.4.1.tar.bz2
CD VALGRIND-3.4.1/
./configure--prefix=/usr/local/webserver/valgrind
Make
Make install



2, use example: to the "LS" program process check, return the results of "definitely lost:0 bytes in 0 blocks." Indicates no memory leak.

[Email protected]/]#/usr/local/webserver/valgrind/bin/valgrind--tool=memcheck--leak-check=full ls/
==1157== Memcheck, a memory error detector.
==1157== Copyright (C) 2002-2008, and GNU GPL ' d, by Julian Seward et al.
==1157== Using Libvex Rev 1884, a library for dynamic binary translation.
==1157== Copyright (C) 2004-2008, and GNU GPL ' d, by Openworks LLP.
==1157== Using valgrind-3.4.1, a dynamic binary instrumentation framework.
==1157== Copyright (C) 2000-2008, and GNU GPL ' d, by Julian Seward et al.
==1157== for more details, rerun with:-V
==1157==
Bin Data0 Dev Home lib64 media mnt opt root selinux sys tcsql.db.idx.pkey.dec ttserver.pid var
Boot Data1 etc lib Lost+found misc net proc sbin SRV tcsql.db tmp usr
==1157==
==1157== ERROR summary:0 errors from 0 contexts (Suppressed:5 from 1)
==1157== malloc/free:in use at exit:28,471 bytes in blocks.
==1157== malloc/free:166 Allocs, Frees, 51,377 bytes allocated.
==1157== for counts of detected errors, rerun with:-V
==1157== searching for pointers to not-freed blocks.
==1157== checked 174,640 bytes.
==1157==
==1157== LEAK SUMMARY:
==1157== definitely lost:0 bytes in 0 blocks.
==1157== possibly lost:0 bytes in 0 blocks.
==1157== still reachable:28,471 bytes in blocks.
==1157== suppressed:0 bytes in 0 blocks.
==1157== Reachable blocks (those to which a pointer is found) is not shown.
==1157== to see them, rerun with:--leak-check=full--show-reachable=yes



3. Use example: a "httptest" program process check written using the Libevent library returns "definitely lost:255 bytes in 5 blocks." Indicates a memory leak occurred.

[Email protected] tcsql-0.1]#/usr/local/webserver/valgrind/bin/valgrind--tool=memcheck--leak-check=full. Httptest
==1274== Memcheck, a memory error detector.
==1274== Copyright (C) 2002-2008, and GNU GPL ' d, by Julian Seward et al.
==1274== Using Libvex Rev 1884, a library for dynamic binary translation.
==1274== Copyright (C) 2004-2008, and GNU GPL ' d, by Openworks LLP.
==1274== Using valgrind-3.4.1, a dynamic binary instrumentation framework.
==1274== Copyright (C) 2000-2008, and GNU GPL ' d, by Julian Seward et al.
==1274== for more details, rerun with:-V
==1274==
==1274== ERROR summary:0 errors from 0 contexts (suppressed:1005 from 2)
==1274== malloc/free:in use at exit:402,291 bytes in blocks.
==1274== malloc/free:15,939 Allocs, 15,865 Frees, 6,281,523 bytes allocated.
==1274== for counts of detected errors, rerun with:-V
==1274== searching for pointers to not-freed blocks.
==1274== checked 682,468,160 bytes.
==1274==
==1274== 255 bytes in 5 blocks is definitely lost in loss record 32
==1274== at 0x4a05fbb:malloc (vg_replace_malloc.c:207)
==1274== by 0x3c1d809bc6:evhttp_decode_uri (http.c:2105)
==1274== by 0x401c75:tcsql_handler (In/data0/tcsql/cankao/tcsql-0.1/tcsql)
==1274== by 0x3c1d80c88f:evhttp_get_body (http.c:1582)
==1274== by 0x3c1d8065f7:event_base_loop (event.c:392)
==1274== by 0x403e2f:main (In/data0/tcsql/cankao/tcsql-0.1/tcsql)
==1274==
==1274== LEAK SUMMARY:
==1274== definitely lost:255 bytes in 5 blocks.
==1274== possibly lost:0 bytes in 0 blocks.
==1274== still reachable:402,036 bytes in blocks.
==1274== suppressed:0 bytes in 0 blocks.
==1274== Reachable blocks (those to which a pointer is found) is not shown.
==1274== to see them, rerun with:--leak-check=full--show-reachable=yes



Check the Httptest program and find that there is a "char *decode_uri = Evhttp_decode_uri (Evhttp_request_uri (req));" The "Decode_uri" in the "no" is not free, and then the process is completed, and then add the "Decode_uri"; , and then using the Valgrind check, the result is already "definitely lost:0 bytes in 0 blocks."

Linux/C + + memory leak Detection Tool: Valgrind

Related Article

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.