现在的位置: 首页 > 综合 > 正文

Qt 在VS中内存泄露 检查 Qt在VS中的内存泄露检测

2014年03月09日 ⁄ 综合 ⁄ 共 3437字 ⁄ 字号 评论关闭

Qt在VS中的内存泄露检测

分类:
C++
QT
随想&&感想

938人阅读
评论(2)
收藏
举报

 今天在QT_VP中简单地添加了内存泄露检测语句:

_CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );

结果出来一大堆的内存泄露,着实吓了一跳,跟踪了一天,逐段派出,最后还是感觉没问题(还是比较自信代码质量的O(∩_∩)O哈哈~)。。。最后上网一查,在vs中,先是进行内存泄露报告,然后才卸载Qt的dll,释放申请的资源。所以才会出现这种情况。

 

标志内存检测报告:setdebugnew.h

注意,只有在cpp中添加setdebugnew.h,而且有new操作的那些函数或者类被调用,才会进行内存泄露跟踪

  1. #ifndef SET_DEBUG_NEW_H  
  2. #define SET_DEBUG_NEW_H  
  3.   
  4. #ifdef _DEBUG  
  5.     #define DEBUG_CLIENTBLOCK   new( _CLIENT_BLOCK, __FILE__, __LINE__)  
  6. #else  
  7.     #define DEBUG_CLIENTBLOCK  
  8. #endif  
  9.   
  10. #define _CRTDBG_MAP_ALLOC  
  11. #include <crtdbg.h>  
  12.   
  13. #ifdef _DEBUG  
  14.     #define new DEBUG_CLIENTBLOCK  
  15. #endif  
  16.   
  17. #endif  

在cpp中添加该头文件以后,内存泄露报告格式:

  1. ./mainwindow.cpp(530) : {24737} client block at 0x00C12420, subtype 0, 8 bytes long.  
  2. ./mainwindow.cpp(367) : {6653} client block at 0x00B59E68, subtype 0, 8 bytes long.  

 然后在main函数中添加一个内存报告过滤器,将不是"*.cpp"格式的报告过滤掉,即可~

 

过滤器头文件reportinghook.h:

  1. #pragma once  
  2.   
  3. #if defined(WIN32)  
  4. void setFilterDebugHook(void);  
  5. #endif   

 reportinghook.cpp:

  1. #if defined(WIN32)  
  2.   
  3. #include <string.h>  
  4. #include "crtdbg.h"  
  5.   
  6. #define FALSE   0  
  7. #define TRUE    1  
  8.   
  9. _CRT_REPORT_HOOK prevHook;  
  10.   
  11. int reportingHook(int reportType, char* userMessage, int* retVal)  
  12. {  
  13.     // This function is called several times for each memory leak.  
  14.     // Each time a part of the error message is supplied.  
  15.     // This holds number of subsequent detail messages after  
  16.     // a leak was reported  
  17.     const int numFollowupDebugMsgParts = 2;  
  18.     static bool ignoreMessage = false;  
  19.     static int debugMsgPartsCount = 0;  
  20.   
  21.     // check if the memory leak reporting starts  
  22.     if ((strncmp(userMessage,"Detected memory leaks!/n", 10) == 0)  
  23.         || ignoreMessage)  
  24.     {  
  25.         // check if the memory leak reporting ends  
  26.         if (strncmp(userMessage,"Object dump complete./n", 10) == 0)  
  27.         {  
  28.             _CrtSetReportHook(prevHook);  
  29.             ignoreMessage = false;  
  30.         } else  
  31.             ignoreMessage = true;  
  32.   
  33.         // something from our own code?  
  34.         if(strstr(userMessage, ".cpp") == NULL)  
  35.         {  
  36.             if(debugMsgPartsCount++ < numFollowupDebugMsgParts)  
  37.                 // give it back to _CrtDbgReport() to be printed to the console  
  38.                 return FALSE;  
  39.             else  
  40.                 return TRUE;  // ignore it  
  41.         } else  
  42.         {  
  43.             debugMsgPartsCount = 0;  
  44.             // give it back to _CrtDbgReport() to be printed to the console  
  45.             return FALSE;  
  46.         }  
  47.     } else  
  48.         // give it back to _CrtDbgReport() to be printed to the console  
  49.         return FALSE;  
  50. };  
  51.   
  52. void setFilterDebugHook(void)  
  53. {  
  54.     //change the report function to only report memory leaks from program code  
  55.     prevHook = _CrtSetReportHook(reportingHook);  
  56. }  
  57.   
  58. #endif   

测试程序:

  1. #include "setdebugnew.h"  
  2.   
  3. #include "reportingHook.h"  
  4.   
  5. int main(int argv, char *args[])  
  6. {  
  7.     _CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );  
  8.     QApplication app(argv, args);  
  9.   
  10.     MainWindow* mainWindow = new MainWindow;  
  11.     mainWindow->setGeometry(30, 30, 1024, 768);  
  12.     mainWindow->show();  
  13.   
  14.     app.connect( &app, SIGNAL(lastWindowClosed()), &app, SLOT(quit()) );  
  15.   
  16.     int result = app.exec();  
  17.   
  18. #if defined(WIN32) && defined(_DEBUG)  
  19.     setFilterDebugHook();  
  20. #endif  
  21.   
  22.     return result ;  

【上篇】
【下篇】

抱歉!评论已关闭.