码迷,mamicode.com
首页 > 其他好文 > 详细

在VC中检测内存泄漏

时间:2015-10-01 20:35:21      阅读:313      评论:0      收藏:0      [点我收藏+]

标签:

声明:checkleaks.h和checkleaks.cpp这两个文件中的代码是在网上COPY的,但原来那个网站现在却找不到了

所以,这篇文章不算完全原创,呵呵。

就当作是一个存档吧

 

先上代码:

 

  1. //checkleaks.h  
  2. #ifndef SET_DEBUG_NEW_H    
  3. #define SET_DEBUG_NEW_H    
  4.   
  5. #ifdef _DEBUG    
  6. #define DEBUG_CLIENTBLOCK   new( _CLIENT_BLOCK, __FILE__, __LINE__)    
  7. #else    
  8. #define DEBUG_CLIENTBLOCK    
  9. #endif    
  10.   
  11. #define _CRTDBG_MAP_ALLOC    
  12. #include <crtdbg.h>    
  13.   
  14. #ifdef _DEBUG    
  15. #define new DEBUG_CLIENTBLOCK    
  16. #endif    
  17.   
  18. #pragma once    
  19.   
  20. #if defined(WIN32)    
  21. void setFilterDebugHook(void);    
  22. #endif   
  23.   
  24. #endif    
  25.   
  26.   
  27. //checkleaks.cpp  
  28. #if defined(WIN32)    
  29.   
  30. #include <string.h>    
  31. #include "crtdbg.h"    
  32.   
  33. #define FALSE   0    
  34. #define TRUE    1    
  35.   
  36. _CRT_REPORT_HOOK prevHook;    
  37.   
  38. int reportingHook(int reportType, char* userMessage, int* retVal)    
  39. {    
  40.     // This function is called several times for each memory leak.    
  41.     // Each time a part of the error message is supplied.    
  42.     // This holds number of subsequent detail messages after    
  43.     // a leak was reported    
  44.     const int numFollowupDebugMsgParts = 2;    
  45.     static bool ignoreMessage = false;    
  46.     static int debugMsgPartsCount = 0;    
  47.   
  48.     // check if the memory leak reporting starts    
  49.     if ((strncmp(userMessage,"Detected memory leaks!/n", 10) == 0)    
  50.         || ignoreMessage)    
  51.     {    
  52.         // check if the memory leak reporting ends    
  53.         if (strncmp(userMessage,"Object dump complete./n", 10) == 0)    
  54.         {    
  55.             _CrtSetReportHook(prevHook);    
  56.             ignoreMessage = false;    
  57.         } else    
  58.             ignoreMessage = true;    
  59.   
  60.         // something from our own code?    
  61.         if(strstr(userMessage, ".cpp") == NULL)    
  62.         {    
  63.             if(debugMsgPartsCount++ < numFollowupDebugMsgParts)    
  64.                 // give it back to _CrtDbgReport() to be printed to the console    
  65.                 return FALSE;    
  66.             else    
  67.                 return TRUE;  // ignore it    
  68.         } else    
  69.         {    
  70.             debugMsgPartsCount = 0;    
  71.             // give it back to _CrtDbgReport() to be printed to the console    
  72.             return FALSE;    
  73.         }    
  74.     } else    
  75.         // give it back to _CrtDbgReport() to be printed to the console    
  76.         return FALSE;    
  77. };    
  78.   
  79. void setFilterDebugHook(void)    
  80. {    
  81.     //change the report function to only report memory leaks from program code    
  82.     prevHook = _CrtSetReportHook(reportingHook);    
  83. }    
  84.   
  85. #endif     
  86.   
  87.   
  88. //main.cpp  
  89. #include <windows.h>  
  90. #include <stdio.h>  
  91. #include "checkleaks.h"  
  92.   
  93. int main()  
  94. {  
  95.     //一定要加上这句,而且要在DEBUG模式下才会有报告输出  
  96.     _CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );    
  97.     int *arr;  
  98.     arr = new int;  
  99.     return 0;  
  100. }  


程序输出:

 

 

    1. Detected memory leaks!  
    2. Dumping objects ->  
    3. e:\vs2008\try\ctemp\ctemp\main.cpp(9) : {62} client block at 0x00503DE8, subtype 0, 4 bytes long.  
    4.  Data: <    > CD CD CD CD   
    5. Object dump complete.  
    6. The program ‘[3348] ctemp.exe: Native‘ has exited with code 0 (0x0).  

http://blog.csdn.net/small_qch/article/details/6856445

在VC中检测内存泄漏

标签:

原文地址:http://www.cnblogs.com/findumars/p/4851483.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!