Use CRT in VC for Memory leakage detection

Source: Internet
Author: User
Transferred from msdn

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:

Copy code
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
Note:

# IncludeThe 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 is only available in the debug version (where_ Debug. Release a normal versionMallocAndFreeFunction.

# DefineThe 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:

Copy code
_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:

Copy code
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_mapallocStatement, the memory leakage dump will be as follows:

Copy code
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_allocThe 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_allocThe 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, it is very easy to call _ crtdumpmemoryleaks. If the program exits from multiple locations, you do not need to place_ CrtdumpmemoryleaksBut can include the following calls at the beginning of the program:

Copy code
_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_dfAnd_ Crtdbg_alloc_mem_dfTwo Bit fields, as shown above.

Set the CRT report mode

By default,_ CrtdumpmemoryleaksDump Memory leakage information to the debug pane of the output window, as described above. Available_ CrtsetreportmodeReset 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:

Copy code
_CrtSetReportMode( _CRT_ERROR, _CRTDBG_MODE_DEBUG );

For more information, see _ crtsetreportmode.

 

Appendix how to write information to a file:

1. Add a header file

# DEFINE _ crtdbg_map_alloc
# Include <stdlib. h>
# Include <crtdbg. h>

 

2. Add at the beginning of the program

_ Crtsetdbgflag (_ crtdbg_report_flag );

 

3. When the program exits, add

 HANDLE hLogFile;
hLogFile = CreateFile("c://log.txt", GENERIC_WRITE,
FILE_SHARE_WRITE, NULL, CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL, NULL);

_CrtSetReportMode( _CRT_WARN , _CRTDBG_MODE_FILE);
_CrtSetReportFile( _CRT_WARN , hLogFile);

_CrtDumpMemoryLeaks();


CloseHandle(hLogFile); 

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.