Capture memory leak in VS2008 (GO)

Source: Internet
Author: User

Memory leaks are annoying, and capturing memory leaks is more annoying ...

In fact, vs itself has a memory leak detection mechanism. Just do the following to turn it on. (You must also run the program in debug mode and exit with normal process)

View Plaincopy to Clipboard
    1. Add the following definitions and header files in the Import function cpp
    2. #define Crtdbg_map_alloc
    3. #include <stdlib.h>
    4. #include <crtdbg.h>
    5. Add the following code at the beginning of the entry function
    6. _CrtDumpMemoryLeaks ();
    7. _CrtSetDbgFlag (_CRTDBG_ALLOC_MEM_DF | _crtdbg_leak_check_df);
[CPP]View Plaincopy
    1. Add the following definitions and header files in the Import function cpp
    2. #define Crtdbg_map_alloc
    3. #include <stdlib.h>
    4. #include <crtdbg.h>
    5. Add the following code at the beginning of the entry function
    6. _CrtDumpMemoryLeaks ();
    7. _CrtSetDbgFlag (_CRTDBG_ALLOC_MEM_DF | _crtdbg_leak_check_df);

Then, after exiting the program in the normal process, the memory leak information is displayed in the Output window.

Attention! The point has come.

Although vs detects where there is a memory leak, it is possible that you have only two valid information (below)

1. The amount of memory not released. (Green section)

2. This block of memory is the total number of applications in the program. (Blue section).

3. The location of the code where the application space is located may not be displayed (Strikethrough section).

Detected memory leaks! Dumping objects, C:/Program files/visual studio/myprojects/leaktest/leaktest.cpp (): {$} normal block at 0x00780e Bytes long. Data: < > CD CD CD CD CD cds CD CD CDS CD CDS CD CD CD Cdobject dump complete.

Well, if this happens, there's no way to know what's wrong. So what should we do at this time?

This is the time to use this function.

View Plaincopy to Clipboard
    1. Num is the memory leak just detected in the local curly braces numbers
    2. _CrtSetBreakAlloc (num);
[CPP]View Plaincopy
    1. Num is the memory leak just detected in the local curly braces numbers
    2. _CrtSetBreakAlloc (num);

The purpose of this function is that when you run the program, the breakpoint will be applied to the space at the time of page Num.

So you need to run the program again. (as long as the process is the same, the memory request order is definitely the same, so it will be properly broken down where needed.) )

Note that the value in curly braces in the memory leaks previously indicated is the space leak of the first few applications.

By looking at the stack call at this point, you know exactly where the memory was applied but forgot to release it. Then release it when it's right in the block.

Capture memory leak in VS2008 (GO)

Related Article

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.