How VC (Configuration) enables memory leak detection

Source: Internet
Author: User

The primary tools for detecting memory leaks are the debugger and the C run-time library (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>
Description

#include statements must take the order shown above. If you change the order, the function that you use may not work correctly.

By including Crtdbg.h, the malloc and free functions are mapped to their "Debug" versions _malloc_dbg and _free_dbg, which track memory allocations and releases. This mapping occurs only in the debug version (where _DEBUGis 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. The statement is not absolutely necessary, but the memory leak dump contains less useful information if the statement is not available.

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

_CrtDumpMemoryLeaks ();

When you run the program under the debugger, _CrtDumpMemoryLeaks displays the memory leak information in the Output window. The memory leak information is as follows:

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 cds CD CD CDS CD CDS CD CD CD                cdobject dump complete.

If you do not use the #define _CRTDBG_MAPALLOC statement, the memory leak dump will resemble the following:

Detected memory leaks! Dumping Objects->{18} normal block at 0x00780e80, bytes long. Data: < > CD CD CD CD CD cds CD CD CDS CD CDS CD CD CD                cdobject dump complete.

When _crtdbg_map_alloc is undefined, it displays:

    • The memory allocation number (within curly braces).

    • Block type (normal, client, or CRT).

    • The 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, it also displays the files in which the leaked memory is allocated. The number in parentheses after the file name (20 in this example) is the line number within the file.

Go to the line that allocates memory in the source file
    • In the Output window, double-click the row that contains the file name and line number.

      Or

      In the Output window, select the line that contains the file name and line number, and then press F4.

_CrtSetDbgFlag

If the program always exits in the same location, calling _CrtDumpMemoryLeaks will be very easy. If the program exits from multiple locations, you do not need to place a call to _CrtDumpMemoryLeaks at each possible exit, and you can include the following call at the beginning of the program:

_CrtSetDbgFlag (_CRTDBG_ALLOC_MEM_DF | _crtdbg_leak_check_df);

The statement automatically calls _CrtDumpMemoryLeakswhen the program exits. You must set both the _CRTDBG_ALLOC_MEM_DF and the _crtdbg_alloc_mem_df two bit fields as shown above.

To set the CRT reporting mode

By default,_CrtDumpMemoryLeaks dumps the memory leak information to the Debug pane of the Output window, as described above. You can use _CrtSetReportMode to reset the setting to dump to another location. If you use a library, it can reset the output to another location. In this case, you can use the following statement to set the output location back to the output window:

_CrtSetReportMode (_crt_error, _crtdbg_mode_debug);

For more information, see _CrtSetReportMode.


How VC (Configuration) enables memory leak detection

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.