Vs enable memory leakage detection

Source: Internet
Author: User
Enable Memory leakage detection

 

This topic applies:

Visual Studio version

Visual Basic

C #

C ++

J #

Quick version

No

No

Local Machine

No

Standard Edition

No

No

Local Machine

No

Professional team Edition

No

No

Local Machine

No

The main tool used to detect memory leaks is the debugger and the C Runtime Library (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:

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

By including crtdbg. H
And free
Function ing to its "debug" version _ malloc_dbg
And _ free_dbg
These functions track memory allocation and release. This ing is only available in the debug version (where_ Debug
. Release a normal versionMalloc
AndFree
Function.

# Define
The statement maps the base 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
In the "output" window
Memory leakage information is displayed. The memory leakage information is as follows:

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.

If not# DEFINE _ crtdbg_mapalloc
Statement, the memory leakage dump 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 CD CD
Object dump complete.

Undefined_ Crtdbg_map_alloc
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 ).

Defined_ Crtdbg_map_alloc
The leaked memory files are also 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 always exits at the same position, call _ crtdumpmemoryleaks
It will be very easy. If the program exits from multiple locations, you do not need to place_ Crtdumpmemoryleaks
But can include the following calls at the beginning of the program:

_CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );

This statement is automatically called when the program exits._ Crtdumpmemoryleaks
. Must be set at the same time_ Crtdbg_alloc_mem_df
And_ Crtdbg_alloc_mem_df
Two Bit fields, as shown above.

Set the CRT report mode

By default,_ Crtdumpmemoryleaks
Dump Memory leakage information to the debug pane of the output window, as described above. Available_ Crtsetreportmode
Reset the settings to dump to another location. If the library is used, it can reset the output to another position. In this case, you can use the following statement to set the output position back to the "output" window:

_CrtSetReportMode( _CRT_ERROR, _CRTDBG_MODE_DEBUG );

For more information, see _ crtsetreportmode
.

See

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.