[VS] Use Visual Studio to find and locate memory leaks @Windows

Source: Internet
Author: User

One, the code example

1#include <stdio.h>2 3 void*memleak1 ();4 void*Memleak2 ();5 6 intMain ()7 {8     void*P1 =memleak1 ();9     void*P2 =Memleak2 ();Ten  Oneprintf"p1=%p, p2=%p\n", p1, p2); A  -     return 0; -}
main.c
1 /*  */2 #include <stdlib.h>34void* memleak1 ()  5{6     return malloc (1); 7 }
memleak1.c
1 /*  */2 #include<stdlib.h> 34void* memleak2 () 5 {6     return malloc (2); 7 }
memleak2.c

Second, how to find out if there is a memory leak

First step: Paste the following code into the source file where the main () function resides:

<stdlib.h><crtdbg.h> #ifndef debug_new#define debug_new NEW (_normal_block, _ _file__, __line__)#define new debug_new#endif#endif

The second step is to paste the following code at the beginning of the main () function:

Char *|  = (char*) malloc (+);

Finally, recompile debug mode and run vs project, you can see the prompt in the Output window:

Detected memory leaks!Dumping Objects-{ About} Normal block at0x00171580, -bytesLong. Data:< >CD CD CD CDs CD CD CD CD CD CD -} Normal block at0x00171540,2bytesLong. Data:< >CD CD { Wu} Normal block at0x00171500,1bytesLong. Data:< >CD Object dump complete.

Here, we can see where the memory leaks and the number of leaks.

Third, how to locate the location of memory leaks

The first step is to add the scheduled macros (preprocessor Marco) to the VS Project:

_crtdbg_map_alloc=1

At this time recompile and run the project, we will get this output, indicating that we have positioned 20 bytes leaked from the 25th line in Main.c, which is the code we added to test memory leaks. But we're still not locating two other memory leaks.

Detected memory leaks!Dumping Objects-C:\USERS\JXION\DOCUMENTS\GITHUB\MEMLEAK\MEMLEAK\MAIN.C ( -) : { About} Normal block at0x00401580, -bytesLong. Data:< >CD CD CD CDs CD CD CD CD CD CD -} Normal block at0x00401540,2bytesLong. Data:< >CD CD { Wu} Normal block at0x00401500,1bytesLong. Data:< >CD Object dump complete.

The second step is to add the following code to the beginning of all source files that we suspect have a memory leak:

<stdlib.h><crtdbg.h> #ifndef debug_new#define debug_new NEW (_normal_block, _ _file__, __line__)#define new debug_new#endif#endif

At this point we can position all the memory leaks:

Detected memory leaks!Dumping Objects-C:\USERS\JXION\DOCUMENTS\GITHUB\MEMLEAK\MEMLEAK\MAIN.C ( -) : { About} Normal block at0x000f1580, -bytesLong. Data:< >CD- CD CD-CD CD CD CD c:\users\jxion\documents\github\memleak\memleak\memleak2.c CD CD CD CD +) : { -} Normal block at0x000f1540,2bytesLong. Data:< >CD CD c:\users\jxion\documents\github\memleak\memleak\memleak1.c ( +) : { Wu} Normal block at0x000f1500,1bytesLong. Data:< >CD Object dump complete.

Iv. Find out if a third-party library has a memory leak:

Invoking a third-party precompiled Library (prebuilt) can also have a memory leak, but there is no way to add the above code to a third-party library to recompile the library. We can only use indirect methods. For example: Print a third-party function call that we think might have a memory leak, and then compare it to the memory leak address in the Output window, as in the example above, where the memory leaks are:

Detected memory leaks!Dumping Objects-C:\USERS\JXION\DOCUMENTS\GITHUB\MEMLEAK\MEMLEAK\MAIN.C ( -) : { About} Normal block at0x002d1580, -bytesLong. Data:< >CD CD CD CDs CD CD CD CD CD CD -} Normal block at0x002d1540,2bytesLong. Data:< >CD CD { Wu} Normal block at0x002d1500,1bytesLong. Data:< >CD Object dump complete.

The memory allocation addresses that are printed at the same time are:

Did you find it? The addresses of the two are consistent, and we can also take this as a guideline for locating memory leaks.

[VS] Use Visual Studio to find and locate memory leaks @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.