Tips on checking and eliminating memory leaks under MFC

Source: Internet
Author: User
Tags contains

Summary

This paper analyzes the Windows environment using MFC debugging Memory leak technology, introduced in Windows environment with VC + + to find, locate and eliminate memory leaks techniques.

Key words: Vc++;crt debug Heap function, heuristic method.

Compiling environment

vc++6.0

Technical principle

The primary tool for detecting memory leaks is the debugger and the CRT debug heap functions. To enable the debug heap function, include the following statement in your program:

#define CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
Note #include statements must take the order shown above. If you change the order, the function you are using may not work correctly.

By including Crtdbg.h, the malloc and free functions are mapped to their "Debug" version _malloc_dbg and _free_dbg, which track memory allocation and release. This mapping occurs only in the debug version (where _DEBUG is defined). The release version uses the normal malloc and free functions.

The #define statement maps the base version of the CRT heap function to the corresponding "Debug" version. This statement is not absolutely required, but if there is no such statement, the memory leak dump contains less useful information.

After you add the statement shown above, you can dump the memory leak information by including the following statement in your program:

_CrtDumpMemoryLeaks();When you run a program under the debugger, _CrtDumpMemoryLeaks displays the memory leak information in the Output window. The memory leak information looks like this:

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.
If you do not use the #define _CRTDBG_MAP_ALLOC statement, the memory leak dump looks 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 CD CD
Object dump complete.

When _CRTDBG_MAP_ALLOC is not defined, the display is:

Memory allocation number (within curly braces).

Block type (normal, client, or CRT).

Memory location in hexadecimal form.

The block size in bytes.

The first 16 bytes of content (also 16 binary).

When _CRTDBG_MAP_ALLOC is defined, a file is also displayed in which to allocate the leaked memory. The number in parentheses after the file name (20 in this example) is the line number within the file.

Go to the line in the source file where memory is allocated

In the Output window, double-click the row that contains the file name and line number.

Or

Select the row that contains the file name and line number in the Output window, and then press the F4 key.

_CrtSetDbgFlag

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.