How to see if a program has a memory leak and locate the memory leak code location (VC + +)

Source: Internet
Author: User

1. What is a memory leak?

Memory leak refers to the dynamic application of memory in the program after use, no release, resulting in this part of the memory is not reclaimed by the system, over time, may cause the program memory is increasing, the system memory is not enough ... Cause a series of disastrous consequences; (see: Memory allocation method for program request memory allocation)

2, 0 tolerance

Troubleshooting a memory leak is especially important for a robust program, especially if the program needs to run for a long time and in a stable manner. The dynamic memory request release of C + + is a language that is controlled by the programmer, with little attention, and most likely there will be memory that has not been freed. This kind of problem, although sometimes just leak a few bytes, but the harm is great. Therefore, we are generally to do: Memory leak 0 tolerance!!!

3. Check and locate memory leaks

Check method:

On the last line of the main function, add a sentence of _CrtDumpMemoryLeaks (). Debug program, naturally close the program to let it exit (do not customize debugging), view the output:

Detected memory leaks!
Dumping objects
{453} normal block at 0x02432ca8, 868 bytes long.
Data: <404303374 > 34 30 34 33 30 33 33 37 34 00 00 00 00 00 00 00
{447} normal block at 0x024328b0, 868 bytes long.
Data: <404303374 > 34 30 34 33 30 33 33 37 34 00 00 00 00 00 00 00
{441} normal block at 0x024324b8, 868 bytes long.
Data: <404303374 > 34 30 34 33 30 33 33 37 34 00 00 00 00 00 00 00
{435} normal block at 0x024320c0, 868 bytes long.
Data: <404303374 > 34 30 34 33 30 33 33 37 34 00 00 00 00 00 00 00
{429} normal block at 0X02431CC8, 868 bytes long.
Data: <404303374 > 34 30 34 33 30 33 33 37 34 00 00 00 00 00 00 00
{212} normal block at 0x01e1bf30, bytes long.
Data: < ' > B3 E1 CD CD CD CD-CD CD-CD CD-CD CD
{204} normal block at 0x01e1b2c8, bytes long.
Data: < > B2 E1 C8 B2 E1 C8 C8 B2 E1 CD CD CD
{138} normal block at 0x01e15680, 332 bytes long.
Data: < > 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
{137} normal block at 0x01e15628, bytes long.
Data: < (V (V > E1) E1 E1 CD CD CD CD
Object dump complete.
Program "[4860] TradeServer.exe: Native" exited with a return value of 0 (0x0).


Take one of the detailed instructions: {453} normal block at 0x02432ca8, 868 bytes long.

The 453 that is surrounded by {} is the memory leak location value We need, 868 bytes long means that there are 868 bits of memory not released in this place.

Next, locate the code location:

In the main function the first line plus:_crtsetbreakalloc(453); This means that the location of the application for 453 of this memory is interrupted. Then debug the program, ... The program was interrupted. Viewing the call stack


Double-click the last function of our code call, and here is Cdbquery::updatedatas (), which locates the code to request the memory:


Well, we finally know where the problem is, and the memory is not released. Change the code and fix this. Then continue ..., knowing that there is no normal block in the debug output, the program has no memory leaks.

Remember to add header files: #include <crtdbg.h>

The last thing to pay attention to is not that there is a normal block must have a memory leak, when your program has global variables, the release of the global variable after the main function exits, so at the end of the main function _ Crtdumpmemoryleaks () will assume that the memory of the global request is not released, causing the illusion of a memory leak. How to avoid it? I usually declare a global variable as a pointer to the main function in the new delete in the main function, and then call _CrtDumpMemoryLeaks (), so that it will not be misjudged.

How to see if a program has a memory leak and locate the memory leak code location (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.