Memory Leak Detection Tool Valgrind Memcheck experience with C + +

Source: Internet
Author: User
Tags valgrind linux

Linux under the Valgrind is really a sharp weapon ah (do not know valgrind please read the reference (1) (2)), help me find a lot of C + + memory management errors, a while ago in the tangle why vs 2013 Running a good program to Linux under the g++ The compile run crashed, giving a bunch of assembly code that I couldn't read. After a long time no solution, think of the memory is definitely a mistake, vs in this respect generally do not check, even if your program is riddled with holes, a variety of memory leaks, memory management errors, as long as it does not affect the operation, did not read the wrong thing to read vs will not tell you (should be vs internal does not implement this memory detection function), So the program written in VS may not be perfect or robust.

Update : Thank you Blog Park kind-hearted netizen @shines77 enthusiastic recommendation, that is, there is memory leak Detection Tool plug-in VLD (Visual leak detector), download installation, installation method Please see the official introduction, use very simple, Add #include <vld.h> to the first entry file and the test report is in the Output window. I installed the use of the next, do not know whether the installation error or what, regardless of the program has no memory leaks, the output is "no memory leaks detected."

The following is my first detection through the valgrind results and a little bit of the result of the changes (not finished, so there are a lot of memory leaks problem ... ):

First Test results: miserable, because the program size is somewhat large.

According to a little bit of modification after the hint, although there are individual errors and memory leak problems, but still in the modification, at least has been able to run successfully ...

Thank Valgrind for helping me to find a heap of memory problems, to find the process also for their own mistakes in the low-level shame, so record down to remember.

1. Most low-level errors: mismatched use of malloc/new/new[] and free/delete/delete[]

This kind of mistake mainly stems from I to C + + new/new[], delete/delete[] mechanism is not familiar with, General new/new[] allocates the memory the type variable I all to use deletes to release, or some variable uses the malloc to assign, When the result is released, it uses delete, which results in many places mismatch, many memory space can not be released. In order to maintain the convenience, I later all use new/new[] and delete/delete[], discard the malloc and free in C.

If you divide the type of user new into basic data types and custom data types, you will be familiar with the following actions and there is no problem.

(1) Basic data types

One-dimensional pointer:

Application space
int *d = new Int[5];
    
Free space
delete[] D;

Two-dimensional pointer:

Application space
int **d = new Int*[5];
for (int i = 0; i < 5; i++)
    d[i] = new INT[10];
    
Free space for
(int i = 0; i < 5; i++)
    delete[] d[i];
Delete[] D;

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.