Summary: This article describes how to use the tool positioning and exclude memory leaks provided by the VC and CRT libraries, and the difficulty of detecting makes the application development of C / C programming languages. Introduction: Dynamic allocation, recycling memory is a strongest feature of C / C programming language, but Chinese philosophers Sun (Sun Tzu, I don't know who it is?) It is the most weaker. This sentence is very correct for C / C applications, where the memory handling error is usually generated by BUGS. A most sensitive and difficult BUG is a memory leak - no memory is successfully released, a small memory leak may not need to pay too much, but the program leaks the bulk memory, or the phenomenon that the increasing leak memory may cause Yes: First, the performance is low, and then the complex memory depletion error is caused. The worst, a memory leak can use so many memory so that other programs have errors, if you leave the user, you can't know where the error comes from. In addition, a harmful memory leak may be a award of another problem. Fortunately, the VC DEBUGER and CRT libraries provide a set of effective tools for detecting and positioning memory leaks. This document describes how to use these tools valid and system exclusion memory leakage.
Start memory leak detection: The main detection tool is the DEBUGER and CRT stack error function. To make the division function take effect, you must include the following statements in your program: #define _crtdbg_map_alloc # include
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 CD CD Object Dump Complete. If you don't use the #define _crtdbg_map_alloc statement, the output information will be as follows:
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. As you can see. When _crtdbg_map_alloc is defined after _CRTDumpMemoryLeaks gives you a lot of useful information. In the case where the _CRTDBG_MAP_ALLOC is not defined, the display information contains: 1. Memory allocation (numbers in the brackets); 2. Memory type (normal type, client type, CRT type); 3.16 credit representation Memory location; 4. The size of the memory is fast; 5. The content of the top 16bytes. If _CRTDBG_MAP_ALLOC is defined, the output information also contains the current leak memory is the positioning information assigned in that file. The numbers in the brackets after the file name are the number of lines. If you double-click this line of information, C: / Program Files / Visual Studio / MyProjects / LeakTest / LeakTest.cpp (20): {18} Normal Block AT 0x00780E80, 64 BYTES Long. Cursor will jump to the original file to assign this Before the memory of memory. Choosing the title in Output is line, press F4 to achieve the same effect. Use using _crtsetdbgflag: If your program's exit point is only one, call _CRTDUMPMEMORYLEAKS will be very easy. However, if your program has multiple exit memories? If you don't want to call _crtdumpMemoryLeaks at each exit point, you can include the following calls in the program: _CRTSETDBGFLAG (_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LOC_CHECK_DF); this statement will automatically call _CRTDUMPMEMORYLEAKS at the end of your program, but you must mention it in front of it The two flags of _CRTDBG_ALLOC_MEM_DF and _CRTDBG_LEAK_CHECK_DF are set.
This paper introduces the type of memory block: As far as it is pointed, a memory leak information indicates that the type of each memory leakage block is normal, client, or CRT type. In practical programs, ordinary and client models are the most common type. Ordinary memory block is the type of memory that is usually allocated. The client-type memory block is a memory block assigned to an object that needs to be designed. The MFC's NEW operation can select a normal or client type as the memory block type as an object to be created. The CRT memory block is a memory block allocated for the CRT library. CRT uses these blocks when handling your own release of memory, so this type is not common in memory leak reports unless a serious exception occurs (for example: CRT library error). There are two types that you can't see in memory leak information: free blocks, it is a memory block that has been released; ignoring blocks, it is a memory block that has been specified.
Set the format of the CRT report: By default, the memory leak information output by _CRTDUMPMEMORYLEAKS is as described above. You can use _CRTSetReportMode to make these output information to other places. If you use a library, it might want to make the output information to other places, in which case you can use _CRTSetReportMode (_CRT_ERROR, _CRTDBG_MODE_DEBUG); the statement restizes the output information to the Output window.
Depending on the memory allocation number setting breakpoint: The file name and the number of files in the memory leak report tells you the location of the memory leak, but knowing that the memory leakage position is not always able to find the problem. One memory allocation operation in a running program may be called multiple times, but memory leaks may only occur in one of them. In order to confirm the problem, you must know the leakage in addition to the location of the leak. The memory allocation number makes it possible to solve this problem. This number is within the brackets after the file name and the number of rows. For example, "18" in the above output is a memory allocation number, which means that the memory leak in your program occurs in the 18th allocation operation. The CRT library is counted on all memory block allocation in the runtime, including its own memory allocation, or other libraries (like MFC). The assignment number of an object is n to indicate the Nth object being allocated, but it may not mean that the Nth object is assigned by code (in most cases they are different). You can set the breakpoint based on the memory allocated number. First set a breakpoint near the program start section, when your program is stopped at the breakpoint, you can set the memory allocation breakpoint through the QuickWatch dialog or Watch window. Enter_crtbreakalloc in the Name column in the Watch window, if you use a multi-threaded DLL version of the CRT library you must contain the context convert {,,, MSVCRTD.DLL} _CRTBREAKALLOC. After pressing, press Enter, Debugger Processes this call, and display the return value in the Value column. If you do not set the memory allocation breakpoint, the return value is -1. Enter the number of allocation you want to set in the Value column, such as 18. You can continue DEBUGGING after you set a breakpoint in the memory allocation location of your own interest. Careful running your program under the same conditions, so that the order of memory allocation is not changed. When the program stops at a specific memory allocation, you can view the Call window and other Debugger information to analyze the conditions for this memory allocation. If necessary, you can continue to run the program, see what this object changes, maybe you can know why memory is not released correctly. Although this operation is very easy, if you are happy, you can set a breakpoint in your code. Add a line of code in the code _CRTBREAKALLOC = 18; it can also be completed by _CRTSetBreakalloc (18).
The method of comparing the other positioning memory leakage method is to capture the "memory snapshot" of the application in important positions. The CRT library provides a structural type_crtMemState, using it you can save a snapshot (current state). _CRTMEMSTATE S1, S2, S3; In order to get a snapshot, you can pass a _crtMemState structure to the _crtMemcheckPoint function, which can populate the current memory state in the structure: _CrtMemCheckpoint (& S1); you can pass the structure _CRTMemState passed to the _CRTMEMDumpStatistics function to output content in the structure. _CRTMEMDUMPSTATISTICS (& S3); It outputs the information as follows: 0 bytes in 0 free block.0 bytes in 0 Normal Blocks.3071 BYtes in 16 Crt Blocks.0 Bytes in 0 ignore blocks.0 bytes in 0 Client Blocks. Largest Number Used: 3071 bytes.total alltes: 3764 BYTES. In order to know if there is a memory leak in a code, you can take a snapshot at the beginning and completion of this code, then call the _CRTMEMDIFerence function to compare two states. : _CrtMemCheckpoint (& s1); // memory allocations take place here_CrtMemCheckpoint (& s2); if (_CrtMemDifference (& s3, & s1, & s2)) _CrtMemDumpStatistics (& s3); as the name implies, _CrtMemDifference compare two memory state, and generating A result (first parameter). Put _CRTMemCheckPoint in the beginning and end of the program, call _CRTMEMDIFerence to compare results, which is also a method of detecting memory leaks. If you find memory leaks, you can use the _CRTMemCheckPoint to divide the program into two halves to detect memory leaks, which is to check the memory leakage using the secondary method.