Tips for checking and eliminating memory leaks in MFC

Source: Internet
Author: User
Author: freepublic Abstract: I'm a newbie, I tested it, but I don't know much about it. I keep doing my research later. This article analyzes the technology of using MFC to debug Memory leakage in Windows, this article describes how to use VC to find, locate, and eliminate memory leaks in Windows. Key words: VC; CRT debugging heap function; test method. Compiling environment VC6.0 Technology

Author: freepublic Abstract: I'm a newbie, I tested it, but I don't know much about it. I keep doing my research later. This article analyzes the technology of using MFC to debug Memory leakage in Windows, this article describes how to use VC to find, locate, and eliminate memory leaks in Windows. Key words: VC; CRT debugging heap function; test method. Compiling environment VC6.0 Technology

Author: freepublic

Abstract:

I'm a newbie, I tested it, but I don't know much about it. I'll keep doing research later.
This article analyzes the technology of debugging Memory leakage Using MFC in Windows, and introduces the methods and techniques of searching, locating, and Eliminating Memory leakage using VC ++ in Windows.
Key words: VC ++; CRT debugging heap function; test method.

Compiling environment
VC ++ 6.0
Technical Principles
The main tool used to detect memory leaks is the debugger and CRT to debug heap functions. To enable the debugging heap function, include the following statements in the program:

# Define CRTDBG_MAP_ALLOC # include <stdlib. h> # include <crtdbg. h>

Note # The include statement must follow the sequence shown above. If the order is changed, the function used may not work properly.

By including crtdbg. h, the malloc and free functions are mapped to its "Debug" version _ malloc_dbg and _ free_dbg, which track memory allocation and release. This ing only occurs in the DEBUG version (where _ DEBUG is defined. The released version uses common malloc and free functions.

# The define statement maps the basic version of the CRT heap function to the corresponding Debug version. This statement is not absolutely required, but without it, the memory leak dump contains less useful information.

After the preceding statements are added, You can include the following statements in the program to dump Memory leakage information:

_CrtDumpMemoryLeaks();

When running a program in the debugger, _ CrtDumpMemoryLeaks will display Memory leakage information in the "output" window. The memory leakage information is as follows:

Detected memory leaks!Dumping objects ->C:PROGRAM FILESVISUAL STUDIOMyProjectsleaktestleaktest.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 CDObject dump complete. 

If the # define _ CRTDBG_MAP_ALLOC statement is not used, the memory leakage dump is 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 CD CD Object dump complete. 

When _ CRTDBG_MAP_ALLOC is not defined, the displayed information is:

Memory Allocation Number (in braces ).
Block Type (common, client, or CRT ).
Memory location in hexadecimal format.
The block size in bytes.
The content of the first 16 bytes (also in hexadecimal format ).
When _ CRTDBG_MAP_ALLOC is defined, files with leaked memory allocated are displayed. The number in the brackets after the file name (20 in this example) is the row number in the file.

Go to the row for memory allocation in the source file

In the "output" window, double-click the row that contains the file name and row number.
-Or-

In the "output" window, select the row that contains the file name and row number, and press F4.

_CrtSetDbgFlag 

If the program exits at the same position, it is convenient to call _ CrtDumpMemoryLeaks. But what if the program can exit from multiple locations? Do not place a call to _ CrtDumpMemoryLeaks at each possible exit. The following calls can be made at the beginning of the program:

_CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF ); 

This statement automatically calls _ CrtDumpMemoryLeaks when the program exits. The _ CRTDBG_ALLOC_MEM_DF and _ CRTDBG_LEAK_CHECK_DF fields must be set at the same time, as shown in the preceding figure.

Description
In the VC ++ 6.0 environment, you do not need to add additional

#define CRTDBG_MAP_ALLOC #include 
 
   #include 
   
  
 

You only need to press F5 and run it in the debugging status. After the program exits, You can see whether there is memory leakage in the "output window. If

Detected memory leaks! Dumping objects -> 

Memory leakage occurs.

Determine the memory leak location
There are two ways to eliminate the memory leakage report:

The first one is relatively simple, that is, the memory leakage has been mapped to the source file. You can directly double-click the row containing the file name and row number in the "output" window. For example

Detected memory leaks! Dumping objects -> C:PROGRAM FILESVISUAL STUDIOMyProjectsleaktestleaktest.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.C:PROGRAM FILESVISUAL STUDIOMyProjectsleaktestleaktest.cpp(20)

Is the source file name and row number.

The second type is troublesome, that is, it cannot be mapped to the source file. Only the block number is allocated in the memory.

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 Object dump complete. 

In this case, I adopt a "test method ". Because the block number allocated by the memory is not fixed, but changes every operation, it is very troublesome to trace. However, I found that although the block number for memory allocation is changed, the changed block number is always the same. That is to say, the block number for memory allocation may be repeated several times. Therefore, this is the basis of the "test method.

  1. Run the program several times in the debugging status, and observe the value of the block number allocated by the memory;
  2. Select the block number with the most frequent occurrences to set the breakpoint, and set the memory allocation breakpoint in the Code: Add the following line (for 18th memory allocations ):
    _crtBreakAlloc = 18; 
    Alternatively, you can use the _ CrtSetBreakAlloc function with the same effect:
    _CrtSetBreakAlloc(18); 

  3. When the breakpoint stops, open the "call stack" window and find the corresponding source code;
  4. Exit the program and observe the memory leakage report in the "Output Window" to check whether the actual block number allocated in the memory is the same as the preset value. If the block number is the same, it is found. If the block number is different, repeat Step 3 until the same.
  5. The last step is to release the allocated memory at an appropriate position based on the actual situation.

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.