Using the debugger and the CRT library to find memory leaks

Source: Internet
Author: User
Tags exit in

Set Memory Leaks Detection <?xml:namespace prefix = o/>

The basic tools for detecting memory leaks are the debugger and the CRT debug heap functions. In order to use the debug heap function, you must include the following instructions in your program:

#define _crtdbg_map_alloc#include <stdlib.h> #include <crtdbg.h>

#include instructions must be described sequentially. If you change the order, the function you use may not work correctly. The _malloc_dbg and _free_dbg that contain crtdbg.h map the malloc and free functions to the beta version, which tracks the allocation and deallocation of memory. This mapping occurs only in a test system (that is, only when _DEBUG is defined). The released system uses the usual malloc and free features.

#define Description maps The low-level version of the CRT heap function to the appropriate test version. This description is not required, but without it, the memory leak contains only information that is not of much use.

Once you have added the instructions just now, you can release the memory information by including the following instructions in your program:

_CrtDumpMemoryLeaks ();

When you run your program in a debug situation, _CrtDumpMemoryLeaks Displays the memory leak information at the Debug tab of the Output window. The memory leak information resembles the following:

Detected memory leaks!

Dumping objects

C:/Program files/visual studio/myprojects/leaktest/leaktest.cpp (): {$} normal block at 0x00780e80, bytes long.

Data: < > CD CD CD CD CD CD CD CD CDs CD CD

Object dump complete.

If you are not using #define _crtdbg_map_alloc instructions, the memory hole is located like this:

Detected memory leaks!

Dumping objects

{The normal block at 0x00780e80, bytes long.

Data: < > CD CD CD CD CD CD CD CD CDs CD CD

Object dump complete.

As you know, when _crtdbg_map_alloc is defined,_crtdumpmemoryleaks gives you more useful information. If _crtdbg_map_alloc is not defined, it will show you the following:

Memory allocation value (in curly braces)

Type of module (normal, client, or CRT)

Memory located in hexadecimal format

Size of the module in bytes

The first 16 bytes of content (can also be in hexadecimal)

When _crtdbg_map_alloc is defined, the displayed content also shows you the file where the leaked memory is allocated. The number in parentheses after the file name (20, for example) is the row value within the file. If you double-click the output line that contains the row value and the file name,

C:/Program files/visual studio/myprojects/leaktest/leaktest.cpp (): {$} normal block at 0x00780e80, bytes long.

The pointer jumps to the row in the source file where the memory is allocated (in the case above, the line number of the Leaktest.cpp is 20). Selecting an output line and pressing F4 will have the same effect.

using _CrtSetDbgFlag

If your program always exists in the same place, it is very easy to call _CrtDumpMemoryLeaks . However, what to do if your program needs to exit in multiple locations. At every possible exit if you do not call _CrtDumpMemoryLeaks, you can include the following call at the beginning of your program:

_CrtSetDbgFlag (_CRTDBG_ALLOC_MEM_DF | _crtdbg_leak_check_df);

This instruction automatically calls _CrtDumpMemoryLeakswhen your program exits. You must set up two bit fields,_crtdbg_alloc_mem_df and _crtdbg_leak_check_df, as previously stated.

Turn translation Memory Mode Block of the class type

As in earlier statements, memory leak information identifies every module that leaks memory as a common module

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.