The ability with dynamic allocation and release of memory is one of the important features of the C / C program language. The Visualc Debugger and CRT libraries offer a range of tools for detecting and identifying memory leaks. To set memory leak detection, the basic tool for detecting memory leaks is the debugger and CRT debug heap function. In order to use the debug heap function, you must contain the following description in your program:
#define _crtdbg_map_alloc # include
_CRTDUMPMEMORYLEAKS (); When running in Debug mode, the DEBUG tab at the Output window will display the information of memory leakage, for example:
Detected memory Leaks! DUMPING OBJECTS -> C: / Program Files / Visual Studio / MyProjects / LeakTest / LeakTest.cpp (20): {18} Normal Block AT 0x00780E80, 64 BYTES Long.Data: <> CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD Object Dump Complete. If there is no #define _crtdbg_map_alloc, the memory leak report will be like this:
Detected memory Leaks! DUMPING OBJECTS -> {18} Normal block at 0x00780e80, 64 bytes long.data: <> CD CD CD CD CD CD CD CD CD CD CD CD CD CD Object Dump Complete. This is visible, defined _ When crtdbg_map_alloc, _CRTDUMPMEMORYLEAKS provides more useful information. If no _crtdbg_map_alloc, the memory leak report is displayed as follows: Memory Allocation Numerical (Praggard) Module Type (Normal, Client or CRT) The size of the internal memory positioned by the hexadecimal format is the first Sixteen bytes of content (or hexadecimal) If the _crtdbg_map_alloc is defined, the reported content also includes files that have allocated the release of memory. The numbers in the parentheses after the file name are the number of rows within the file. At this point, double-click the output line containing the row value and the file name, or select the output line and press F4: C: C: Normal BlockTest / LeakTest.cpp (20): {18} Normal Block AT 0x00780E80, 64 BYTES long. The editing window will jump to the line code that assigns the leak memory in the file, the line number 20 in LeakTest.cpp. Use _CRTSetDbgflag If your program only exits one place, it is very easy to select the location of the call _CRTDUmpMemoryleaks. However, if your program may exit multiple positions in the program? If you don't want to call _crtdumpMemoryLeaks at each possible exit, you can include the following calls: _CRTSETDBGFLAG (_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF); _CRTDUMPMEMORYLEAKS will be called automatically when the program exits _CRTDBG_ALLOC_MEM_DF and _CRTDBG_LEAK_CHECK_DF). The type memory leak report of the translation memory module is divided into ordinary blocks, client blocks, and CRT blocks. In fact, you only need to pay attention to normal blocks and client block types. Normal Block: The memory allocated by your program. CLIENT Block: is a special memory block, which is an object used by the MFC. When the program exits, the sect of the object is not called. The MFC New operator can be used to create normal blocks and client blocks. CRT Block: The memory block allocated by C Runtime Library is used by C Runtime Library. The CRT library manages these memory allocation and release, usually you don't find CRT memory leaks in the memory leak report, unless the program has a serious error (such as CRT library collapse). The following two types of memory blocks do not appear in the memory leak report: Free block: The memory already released (free). Ignore Block: It is a programmer explicitly declared memory blocks that do not appear in the memory leak report. Set the CRT report style usually _CRTDUMPMEMORYLEAKS () will dump memory leakage information to the debug field of the Output window. You can use _CRTSetReportMode () to reset the output to another. For more detailed how to use _CRTSetReportMode (), please check the MSDN.
Setting a breakpoint at the number of memory allocations The file name and the line number in the memory leak report can tell the code location of the split memory, but there are this information for complete understanding of the leak reasons, sometimes not enough. Because a program is running, the code allocated allocation memory will be called many times, but may be the release of the discharge after a certain call. In order to determine that those memory is not released, you must not only know that the inner existence of leaks is allocated, but also know the conditions generated by leakage. For you, the helpful information is the memory allocation number - the value that appears in the pair of parcels after the file name and the line number. For example, in the output information below, "18" is a memory distribution number, meaning the spill of memory is the 18th memory block allocated in your program: detected memory Leaks! DUMPING OBJECTS -> C: / Program Files / Visual Studio / myProjects / LeakTest / LeakTest.cpp (20): {18} Normal Block AT 0x00780E80, 64 BYTES Long.Data: <> CD CD CD CD CD CD CD CD CD CD CD CD CD CD Object Dump Complete. CRT The library is count all memory modules allocated during the program, including CRT you allocate memory or other modules such as MFC. Therefore, an object with the distribution number N is the nth object assigned in your program, but does not mean the Nth object assigned by the code (in most cases, it will not be.) In this case, you can set a breakpoint using the allocation number where the memory is allocated. In order to set such an endpoint, you can set a location breakpoint at the beginning of your program. When your program is at that point break, you can set a location breakpoint from the QuickWatch dialog or Watch window. For example, in the Watch window, type the following expression at the NAME field:
_CRTBREAKALLOC If you are using the multi-threaded version of CRT library Dynamic-Link Library (DLL), you must contain context operators, like this:
{,,, MSVCRTD.DLL} _CRTBreakallo now presses the return key, the debugger finds this value and place the result in the Value column. If you have not set any memory allocation breakpoints during the memory allocation, then this value is -1. Use the allocated value of the memory allocation you want to interrupt, replace the value in the Value table - for example, 18. Continue to debug after the memory allocation breakpoint is set. At this time, be careful when running the program, to ensure that the order in which the memory block allocation is not changed. When your program is interrupted in the memory allocation point, you can view the Call Stack window and other debug information to analyze the reason for the leak. You can still continue from that point, so that what happened, while determining why memory is not released (set up a memory allocation breakpoint is very helpful). Although it is usually easier to set up memory allocation breaks in the debugger, if you like, you can set them in your code. In order to set a memory allocation breakpoint in your code, you can add such a line (for the eighteenth memory allocation):
_CRTBREAKALLOC = 18; you can also use the _CRTSetBreakAlloc function with the same effect:
_CRTSetBREAKALLOC (18); another method of comparing memory status Location memory leaks is to make snapshots on the memory status of the application in the key point. The CRT library provides a structural type_CrtMemState. You can use it to store a snapshot of the memory status: _CRTMemState S1, S2, S3; In order to snapakap the memory status on a specific point, you can pass a _CRTMEMSTATE structure to the He _CRTMemCheckPoint function. This function fills this structure with a snapshot of the memory status at the time:
_CRTMemCheckpoint (& S1); You can pass this structure to the _crtmemdumpstatias function to the contents of the Dump _CRTMEMState structure:
_CRTMEMDUMPSTATISTICS (& S1); This function prints out a bunch of memory allocation information similar to the following:
0 BYtes in 0 Normal Blocks.071 bytes in 16 CRT Blocks.0 Bytes in 0 Client Blocks.0 bytes in 0 Client Blocks.Largest Number Used: 3071 bytes. Total Allocations: 3764 BYTES. To determine Is a memory leak appear in a section code, you can make a quick photo before and after this section, then compare two states with _CRTMEMDIFCERENCE:
_CrtMemCheckpoint (& s1); // memory allocations take place here ...... _CrtMemCheckpoint (& s2); if (_CrtMemDifference (& s3, & s1, & s2)) _CrtMemDumpStatistics (& s3); can be known from the name, _CrtMemDifference used to compare two memory state ( The two parameters of the last side) and return the results of the status difference (the first parameter). The _CRTMemCheckPoint calls and enables _CRTMEMDIFFERENCE to compare the results for the detection of memory leaks. If a leak is detected, you can use the _CRTMemCheckPoint call to split your program and use the binary search technique to locate the leak.