Memory leak detection methods under Windows

Source: Internet
Author: User

Under Windows, you can use Visual C + + 's C Runtime Library (CRT) to detect memory leaks.

First, we insert this section of code in the first line of a. C or. cpp file:

#define _CRTDBG_MAP_ALLOC#include <crtdbg.h>

Insert the following code in main ():

int main() {        //开始的地方插入该语句    _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);    //_CrtSetBreakAlloc(79);    //其它代码        //return前插入该语句    _CrtDumpMemoryLeaks();    return0;}

The output from Visual Studio is as follows:

C:\Temp\memleak\memleak.cpp(15) : {790x00441BA02long4142Object dump complete.

We can add _CrtSetBreakAlloc (79); To debug, it will break in the allocation call at the 79th time, and then the call stack will be able to find the ins and outs.

Memory leak detection methods under Windows

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.