Enable memory leak detection in VC + +

Source: Internet
Author: User

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 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

Normal block at 0x00780e80, bytes long.
Data: < > CD CD CD CD CD CD CD CD CDs CD CD
Object dump complete.

If you do not use the #define _CRTDBG_MAP_ALLOC statement, the memory leak dump is as follows:

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.

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 at the same location, calling _CrtDumpMemoryLeaks is convenient enough, but what if the program can exit from multiple locations? Instead of placing a call to _CrtDumpMemoryLeaks at each possible exit, 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_leak_check_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 information about using _CrtSetReportMode to send output to other locations, see _CrtSetReportMode.

http://blog.csdn.net/seawave/article/details/591129

Enable memory leak detection in VC + +

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.