C ++ Memory leakage detection

Source: Internet
Author: User

There are a variety of methods to detect memory leaks, including the use of memory leak detection tools (such as BoundsChecker) to detect memory leaks; there is a direct view of the code detection code logic to see where the memory has not been released. Generally, static memory leaks are easily discovered through tools and code checks. dynamic memory leaks are hard to be found, generally, you can search by adding breakpoint tracking and Run-Time memory detection tools in the code.
In general, there are several steps to check for Memory leakage:
1. First, you need to control the release of memory when writing code, for example, delete after new, check whether the Destructor is actually executed (many people write the code to release the memory to be processed in the destructor). If the code is not actually executed, the object needs to be released dynamically; some time ago, I used the singleton mode object in a project and set the constructor and destructor to the protection type, when the code exits, It is not executed in the Destructor (I do not know the specific reason), and only the object is manually deleted.
2. Let the program run for a long time to see if the memory of the corresponding program in the task manager has been increasing upwards;
3. Use common memory leak detection tools to detect memory leaks.
This document describes the functions and usage of some memory leak detection tools.
 
1. Memory detection tools/functions provided by the VS2005/VS2008 compiler.
Add the following at the beginning of the main () function:
# Include "crtdbg. h"
_ CrtSetDbgFlag (_ CRTDBG_ALLOC_MEM_DF | _ CRTDBG_LEAK_CHECK_DF );
 
2. Use tools such as BoundsChecker.
BoundsChecker is a Run-Time error detection tool that mainly locates various errors that occur during running of a program. BoundsChecker can detect the following errors:
1. pointer operation and memory and resource leakage errors, such:
Memory leakage;
Resource leakage;
Incorrect operation on Pointer variables.
2. Memory Operation errors, such:
Memory read/write overflow;
Use uninitialized memory.
3. API function usage Error
Detailed information reference: http://www.bkjia.com/kf/201104/89133.html
 
3. in linux, valgrind can be used to detect memory leakage errors.
Detailed information reference: http://www.bkjia.com/kf/201104/89135.html
 
4. purify tool, which is dedicated to detecting memory, including leakage, out-of-bounds, pointer flying, and so on. It is easy to use on VC.
 
5. Use Windbg to check whether the handle is leaked, which is convenient.
 
Vi. Visual Leak Detector
Visual Leak Detector is a free memory Leak detection tool for Visual C ++. It can be downloaded in the http://www.codeproject.com/tools/visualleakdetector.asp. Compared with other memory leak detection tools, it detects memory leaks and has the following features:
1. You can obtain the call stack of the memory leak point. If you can, you can also obtain the file and row number of the memory leak point;
2. You can obtain complete data that exposes the memory;
3. You can set the Memory Leak report level;
4. It is a packaged lib and does not need to compile its source code during use. You only need to make minor changes to your own code;
5. Its source code is released with the GNU license, with detailed documents and comments. It is a good choice for readers who want to learn more about heap memory management.
For details, refer to: aspx "> http://www.codeproject.com/KB/applications/visualleakdetector.aspx

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.