Use V C to detect and isolate memory leakage
hero
C, C (2003-11-03 09:12:56)
-------------------------------------------------- ------------------------------
The ability with dynamic allocation and release of memory is one of the important features of the C / C program language. The Visual C Debugger and CRT libraries offer a range of tools that effectively detect and identify memory leaks.
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 instructions in your program: #define _crtdbg_map_alloc # include
#include Description must be described in order. If you change the order, the function used may not work properly. The _malloc_dbg and _free_dbg containing crtdbg.h map Malloc and Free functions to the beta, which tracks the allocation and release of memory. This mapping only occurs in a test system (that is, when _debug is defined). The released system uses the usual Malloc and Free function.
#DEfine Description Mapping the low version of the CRT heap function to the corresponding test version. This explanation is not required, but without it, the memory leak is only there is not much information.
Once you have added you just now, you can release memory information by including the following instructions in your program: _CRTDUMPMEMORYLEAKS ();
When running the program in the debugging, _CRTDUMPMEMORYLAKS exhibits memory leakage at the DEBUG tab of the output window. Memory Leakage Information Similar to this: 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 CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD.
If you don't use #define _crtdbg_map_alloc, memory vulnerability is similar to the following: 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 CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD OBJECT DUMP Complete.
When _crtdbg_map_alloc is defined, _CRTDUMPMEMORYLEAKS gives you more useful information. If _crtdbg_map_alloc is not defined, then it will be displayed as follows:
Memory Allocation Numerical (Praggies) Module (Normal, Client or CRT) Positioning the memory in the 16-in-format format, the first sixteen-seated content of the byte coupon module (can also use hex System When _CRTDBG_MAP_ALLOC is defined, the displayed content also shows you files allocated by leak memory. The numbers (20, take this as an example for this) after the file name are the number of lines in the file. If you double-click the output line containing rows and file names, c: / program files / visual studio / myprojects / leaktest / leaktest.cpp (20): {18} Normal Block AT 0x00780E80, 64 BYTES long. Pointer will jump to The memory is allocated in the source file (in the above case, the line number of LeakTest.cpp is 20). Select the output row and press F4 to have the same effect. Using _CRTSetDbgflag If your program is always existing in the same place, it is easy to call _CRTDumpMemoryLeaks. However, if your program needs to exit in multiple locations? If _CRTDUMPMEMORYLEAKS is not called at each possible exit, you can include the following calls: _CRTSETDBGFLAG (_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF); When the program exits, this instruction is automatically called _CRTDUMPMEMORYLEAKS. You have to set two bitmas, _crtdbg_alloc_mem_df and _crtdbg_leak_check_df.
The type memory leak information of the translation memory module authenticates each module of the leak memory as an ordinary module, a client module or a CRT module. In fact, ordinary modules and customer modules are the only type you may pay attention to. A normal block is a normal memory allocated by your program. A Client Block is a special memory module that is used by Microsoft Foundation Classes (MFC) due to an object of a destructor. The MFC New operator establishes a normal module or a client module to fit the created module. A CTR module is a memory module allocated by the CRT library. The CRT library manages these modules to manage your own deregistration, so you can't notice this in the memory leak report, unless some places have a serious error (for example, CRT library collapse). There are two module types you have never seen in memory leak information: Free block is a released memory module Ignore Block is that you have already tagged over to the memory leak report. Module.
Setting the CRT report style as before, press the default mode, _CRTDUMPMEMORYLAKS Dumps Memory Leakage Information to the Debug pane of the output window. You can use _CRTSetReportMode to reset it to the stack to another location. If you use a library, it may reset the output to another. In this case, you can use the following description to set the output position back to the output window: _CRTSetReportMode (_CRT_ERROR, _CRTDBG_MODE_DEBUG); About using _CRTSetReportMode to send output information to another location, see the _CRTSetReportMode section of Visual C files .
Setting a breakpoint at the number of memory allocations and the file names and line numbers in the memory leak report can tell you that there is allocated inside the leak, but understanding that there is a distribution there is not always sufficient for identification issues. During a program run, it is often a distribution that will be called many times, but it may leak memory in a certain call. In order to determine the problem, you must not only know the inner existence of the leak, but also know the conditions that leaks. For you, the information that makes it possible is the memory allocation number. This is the value that appears in Curly Brace when the file names and line numbers are displayed. For example, in the output below, "18" is a memory distribution number. It means that the leak memory is the 18th module of memory allocation 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 CD CD Object Dump Complete.CRT library calculates the memory modules allocated during program operation, including CRT you allocate memory or other modules such as MFC. Therefore, an object with the allocation number N is the Nth object assigned in your program, but it is impossible to be nth object assigned by the code. (In most cases, it will not.)
You can set a breakpoint using the assigned number in the memory allocation. To do this, you can start close to your program, set a location breakpoint. When your program is paused, you can set a bit breakpoint from the QuickWatch dialog or Watch window. For example, in the Watch window, type the following expression in the Name column: _CRTBREAKALLOC
If you are using a multi-threaded Dynamic-Link Library version of the CRT library, you must contain context operators, as described here: {, MSVCRTD.DLL} _CRTBreakalloc
Now, press Return. The debugger evaluation is called and placed the result in the Value column. If you have not set any breakpoints in the memory allocation, then this value is -1. Use the allocation value you want to interrupt to replace the value in the Value table - for example, 18 to interrupt the assignment in the output during the output.
When you set a breakpoint after you are interested in the memory allocation, you can continue to debug. Be careful when running the program under the same conditions as before, and thus the order of assignments will not change. When your program is interrupted in a special memory allocation point, you can view the Call Stack window and other test information to determine the allocation of memory under this condition. If needed, you can continue the program from that point so that what happened to the object, and it is also possible to determine that it is not properly assigned. (It is very helpful for setting up a data breakpoint to objects.)
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;
Most option, you can use the _crtsetbreakalloc function with the same effect. _CRTSETBREAKALLOC (18);
Compare memory status
Another way to locate memory leaks is to make snapshots on the memory status of the application in a 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 at 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 _crtmemdumpstatistics function to tip the _CrtMemState structure arbitrarily: _CRTMEMDUMPSTATISTICS (& S3); (& S1);
This function prints out a bunch of memory allocations similar to the following: 0 bytes in 0 free blocks.0 bytes in 0 Normal Blocks.071 Bytes in 16 CRT Blocks.0 Bytes in 0 ignore blocks.0 bytes in 0 Client Blocks. Largest Number Used: 3071 bytes.total allocations: 3764 bytes.
To determine if a memory leak appears in a section code, you can make a quick photo on this section before this section, then compare two states with _CRTMEMDIFerence: _CrtMemCheckpoint (& S1); // Memory Allocations Take Place Here_CrtMemCheckpoint (& S2);
IF (_CRTMEMDIFERENCE (& S3, & S1, & S2)) _CRTMEMDUMPSTATICS (& S3);
Like a name implied, _CRTMEMDIFerence compares two memory status (the first two parameters) and produces a result of different than these two states (third parameter). The _CRTMemCheckPoint calls and makes _CRTMEMDIFCERENCE comparison to detect memory leaks provide another method for detecting memory leaks. If a leak is detected, you can use the _CRTMemCheckPoint call to split your program and use binary binary search technique to locate the leak. Points: Computer World