Find memory leaks with CRT and find memory leaks with CRT

Source: Internet
Author: User

Find memory leaks with CRT and find memory leaks with CRT

Reference original address:

Https://msdn.microsoft.com/en-us/library/x98tx3cf.aspx

 

1. include strictly in the following order in program

  1 #define _CRTDBG_MAP_ALLOC  2 #include <stdlib.h>  3 #include <crtdbg.h>

 

2. It must be a Debug build.

Q: LiteServer debugging may encounter some problems.

A: It can be used to verify whether Memcheck has A false positive.

You can place a breakpoint at the corresponding address (the breakpoint is frequently broken on malloc, And the breakpoint must be set according to the size)

Q: The code may be different in the Release and Debug Versions. In the end, a memory leak with no

 

3. The macro "# define _ CRTDBG_MAP_ALLOC" cannot be omitted. Otherwise, the dump of the memory leak lacks some details (such as the code location information of the leak)

 

4. You can call this function to print the memory leak report before exiting the app.

_ CrtDumpMemoryLeaks ();

Q: If it is a global or static obj, it has not been released before exit. Will the report generate no false positives?

A: No

 

5. If the app has multiple exits, you do not need to call _ CrtDumpMemoryLeaks () at each exit location. The _ CrtSetDbgFlag () under the call position at the beginning of the app will automatically call _ CrtDumpMemoryLeaks () at each exit position ()

_ CrtSetDbgFlag (_ CRTDBG_ALLOC_MEM_DF | _ CRTDBG_LEAK_CHECK_DF );

 

6. By default, the memory leak report will be in the Output window of the VS Studio Debug window. You can use _ CrtSetReportMode () to redirect it to another location.

 

7. Report Format

Detected memory leaks! Dumping objects-> c: \ users \ username \ documents \ projects \ leaktest. cpp (20): {18} // {18}: memory Allocation No. normal block at 0x00780E80, 64 bytes long. // 64 bytes: The block size (not the size specified during allocation, because the allocated heap will add additional information such as the heap tail, data: <> CD CDObject dump complete.
 

Q: It is found that the row information of the memory leak is not output even according to the definition in the first article.

A: This crtdbg does not work well for new. The block address of the crtdbg only calls the macro address. The following macro DBG_NEW can be used to replace new to print the corresponding row information.

#ifdef _DEBUG    #define DBG_NEW new ( _NORMAL_BLOCK , __FILE__ , __LINE__ )    // Replace _NORMAL_BLOCK with _CLIENT_BLOCK if you want the     // allocations to be of _CLIENT_BLOCK type #else    #define DBG_NEW new#endif

 

Q: What should I do if I need to replace all source codes and third-party libraries?

Q: How can I find the source information for multi-layer calls without stack tracing information?

A: Debug. After the app entry is disconnected, enter "_ crtBreakAlloc" in the watch window (if "Runtime Library" is "/MD", add "{,, ucrtbased. dll} _ crtBreakAlloc "). The value should be"-1 "and be changed to the allocation sequence number of the memory leak generated by detect (for example, {18} in 7 }), memory of this size is disconnected (note that the re-run condition must be the same as that obtained for the first time ).

In addition, you can also specify the following in the code:

_crtBreakAlloc = 18;    or :_CrtSetBreakAlloc(18);

 

8. Test

1> code

#include <memory>std ::tr1 ::shared_ptr <int >   sp_nTest;void  Test (){      sp_nTest. reset( new int( 0x88));      //memory leak      int*  pnTest = new int( 0xCC);      void* pMalloc = malloc( sizeof( int));}

 

2> result

Detected memory leaks!Dumping objects ->d:\codes\vs2010\test\detectmemleak\console\test.cpp(14) : {65} normal block at 0x007B18A8, 4 bytes long.Data: <    > CD CD CD CD
{64} normal block at 0x007B4F90, 4 bytes long.
Data: <    > CC 00 00 00Object dump complete.

The red part is the new memory leak.

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.