Valgrind Test Memory leaks

Source: Internet
Author: User
Tags valgrind

Explanation of five kinds of memory loss in Valgrind memory detection

Results Sample:
Leak SUMMARY:
Definitely lost:140 bytes in 5 blocks.
Indirectly lost:1,252 bytes in blocks.
Possibly lost:0 bytes in 0 blocks.
Still reachable:36 bytes in 1 blocks.
suppressed:0 bytes in 0 blocks.

1) still reachable: Indicates that the leaking memory is still pointing to it when the program is running, so that the internal program can be released before it is finished running. In general, Valgrind does not report this leakage unless the parameter--show-reachable=yes is used.

2) Definitely LOSG: Clearly has been leaked, because when the program runs out, no pointer to it, pointing to its pointer in the program lost. Generally this kind of leakage is common, and it is more difficult to find the problem.

3) indirectly lost: indirectly????????????????

4) possibly lost: found a pointer to the middle of a block of memory, rather than pointing to the memory block head. This pointer is usually pointed to the memory block head, and then moved to the middle of the memory block, there may be no relationship between the pointer and the memory, the detection tool is only suspected of a memory leak.

5) Suppressed:

Http://hi.baidu.com/andrewnwpu/blog/item/006a770e577785c17bcbe1bc.html

Memory Leaks

Problem Description:

Memory leak (Memory leak) refers to the dynamically requested memory in a program that is not released after use, and cannot be accessed by other parts of the program. Memory leaks are the most vexing problem in developing large programs, so that some people say that memory leaks are unavoidable. In fact, to prevent memory leaks from doing good programming habits, the other important thing is to strengthen unit testing (test), and Memcheck is such a good tool.

The following is a typical memory leak case. The main function calls the MK function to generate the tree node, but after the call completes, there is no corresponding function: Nodefr frees the memory so that the tree structure in memory cannot be accessed by other parts, causing a memory leak.

In a separate function, everyone's memory leak awareness is relatively strong. But in many cases, we will do some packaging for malloc/free or new/delete to meet our specific needs and not be used and released in a single function. This example also illustrates where memory leaks are most likely to occur: The two-part interface section, a function to request memory, and a function to free memory. And these functions are developed and used by different people, which makes memory leaks more likely. This requires a good unit test habit, which eliminates memory leaks in the initial phase.


Listing 11


Listing 11.2


Listing 11.3

Results Analysis:

Assuming that the above file fame tree.h, Tree.cpp, badleak.cpp, the resulting executable program is badleak and tested with Memcheck, the output is as follows.


Listing 12

The sample program is the process of generating a tree with a size of 12 (considering memory alignment) and a total of 8 nodes. As can be seen from the above output, all memory leaks are found. Memcheck divides memory leaks into two types, one is a possible memory leak (possibly lost) and the other is a determined memory leak (definitely lost). Possibly lost refers to the existence of a pointer that is still able to access a block of memory, but that pointer is no longer the first address of that memory. Definitely lost refers to the ability to access this block of memory. The definitely lost is also divided into two types: direct and indirect (indirect). The direct and indirect difference is that there is no pointer to the memory directly, which means that the pointer to that memory is located at the memory leak. In the above example, the root node is directly lost, while the other nodes are indirectly lost.





Back to the top of the page

Summarize

This paper introduces the architecture of Valgrind, and emphatically introduces its most widely used tools: Memcheck. This paper expounds the basic principle, basic use method of Memcheck discovering memory problem, and how to find out the five kinds of memory problems in the current development of Memcheck. Discovering memory problems early in the project can greatly improve development efficiency, and Valgrind is an excellent tool to help you achieve this goal.

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.