VS C + + memory leak detection

Source: Internet
Author: User

Reference article:

Http://msdn.microsoft.com/zh-cn/library/x98tx3cf.aspx

Http://www.cnblogs.com/jianqiang2010/archive/2010/12/02/1894327.html

Http://www.cnblogs.com/skynet/archive/2011/02/20/1959162.html

1. Add the following code to the file where the Main method resides:

1 //you can navigate to the file where the memory leak occurs and the specific line that detects the memory allocated by malloc2 #define_crtdbg_map_alloc3#include <stdlib.h>4#include <crtdbg.h>5 6 //Save the information allocated to the memory, you can navigate to that line of memory leaks. Used to detect the memory allocated by new7 #ifdef _DEBUG8 #defineNew New (_normal_block, __file__, __line__)9 #endifTen  One //Useful AInlinevoidEnablememleakcheck () - { -     //This statement automatically calls _CrtDumpMemoryLeaks () when the program exits, for multiple exit exits. the     //If there is only one exit location, you can call _CrtDumpMemoryLeaks () before the program exits -_CrtSetDbgFlag (_CrtSetDbgFlag (_crtdbg_report_flag) |_CRTDBG_LEAK_CHECK_DF); -}

2, call the Enablememleakcheck ()   method where it starts in the main method. So far, some of the memory leaks that can be detected can be targeted to specific files and rows, but there are some other projects or other files where there is a memory leak or no specific files and rows, which are seen in the Output window of vs. As the output below, the first leak has very specific positioning information, the second leak is not.

1 q:\project_wydxml\wydxmlcreator\project_win32\wydxmlcreator.cpp (%): {  0x00abbc40along. 2  Data: <                >3 {2540x00abf6f8long  . 4  Data: <          

3, for those who do not locate the specific location of the memory leak processing is as follows: Record the memory is allocated several times, such as the third row above and the fourth row indicates a second memory leak, the third row {} 254 indicates that the memory of the leak is assigned the No. 254 time. End run, after the Enablememleakcheck () method, add a sentence code: _CrtSetBreakAlloc (254); The parameter in parentheses is the assigned ordinal of the leaked memory that is not accurately located, the other is not changed, run again, the program will be allocated in the NO. 254 block of memory into the breakpoint, through the breakpoint stack can be more convenient to know which leaks.

4, note: For the new out of some of the custom classes of objects, directly call its destructor, Enablememleakcheck will still consider it as not free memory, to call the delete xxx directly line ...  such as: a *a = new A (); A->~a (); Will be sentenced to do a problem, delete A is considered as no problem ....

5, outside, there is a piece of code to monitor whether there is a memory leak, as follows. If there is a leak in this code, _CrtMemDifference will compare it and then _crtmemdumpstatistics output the relevant information. The output of the information is not specifically targeted to the specific line of the file, but sometimes there are some auxiliary functions of it

1 _crtmemstate S1, S2, S3; 2 _crtmemcheckpoint (&S1); 3 4 // Your code 5 6 crtmemcheckpoint (&S2); 7 if (_CrtMemDifference (&S3, &s1, &S2)) 8      _CrtMemDumpStatistics (&S3);

The full code is as follows:

1#include"stdafx.h"2#include <iostream>3#include"wydxml.h"4 5 //you can navigate to the file where the memory leak occurs and the specific line that detects the memory allocated by malloc6 #define_crtdbg_map_alloc7#include <stdlib.h>8#include <crtdbg.h>9 Ten //Save the information allocated to the memory, you can navigate to that line of memory leaks. Used to detect the memory allocated by new One #ifdef _DEBUG A #defineNew New (_normal_block, __file__, __line__) - #endif -  the //Useful -InlinevoidEnablememleakcheck () - { -     //This statement automatically calls _CrtDumpMemoryLeaks () when the program exits, for multiple exit exits. +     //If there is only one exit location, you can call _CrtDumpMemoryLeaks () before the program exits -_CrtSetDbgFlag (_CrtSetDbgFlag (_crtdbg_report_flag) |_CRTDBG_LEAK_CHECK_DF); + } A  at  - int_tmain (intARGC, _tchar*argv[]) - { -      - Enablememleakcheck (); -  in     //run to the 191th memory allocation and stop. -     //_CrtSetBreakAlloc (191); to  +     Char* p =New Char[ -]; -     Char* P1 =New Char[ $]; the     Char* P2 = (Char*) malloc ( -); * delete p; $ Panax Notoginseng GetChar (); -  the     //_CrtDumpMemoryLeaks ();//This code seems to be outputting extra memory allocation information. +     return 0; A}

This article was previously linked to: http://www.cnblogs.com/zouzf/p/4152279.html

VS C + + memory leak detection

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.