如何在 Windows 上检测 QtCreator 中的内存泄漏?

新手上路,请多包涵

如何在 Windows 上检测 QtCreator 中的内存泄漏?在文档中,他们推荐 Memcheck,但它仅适用于 Mac 和 Linux。对 Windows 有什么建议吗?

原文由 laurent 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.1k
1 个回答

经过多次尝试,我终于找到了一种在 Windows 上检测 Qt 项目的内存泄漏的方法:

  1. 首先,它不能直接在 Qt Creator 中完成,因此您需要创建一个 Visual C++ 项目来进行内存泄漏检测。值得庆幸的是,qmake 让这一切变得简单。打开 Qt SDK 命令行工具并运行:
 qmake -spec win32-msvc2008 -tp vc

这会将您的项目转换为 .vcproj。

2)打开这个项目,添加必要的内存泄漏检测代码:

到您的 main.cpp 文件:

 // Necessary includes and defines for memory leak detection:
#ifdef _MSC_VER
#define _CRTDBG_MAP_ALLOC
#include <crtdbg.h>
#endif // _MSC_VER

#if defined(_MSC_VER)

// Code to display the memory leak report
// We use a custom report hook to filter out Qt's own memory leaks
// Credit to Andreas Schmidts - http://www.schmidt-web-berlin.de/winfig/blog/?p=154

_CRT_REPORT_HOOK prevHook;

int customReportHook(int /* reportType */, char* message, int* /* returnValue */) {
  // This function is called several times for each memory leak.
  // Each time a part of the error message is supplied.
  // This holds number of subsequent detail messages after
  // a leak was reported
  const int numFollowupDebugMsgParts = 2;
  static bool ignoreMessage = false;
  static int debugMsgPartsCount = 0;

  // check if the memory leak reporting starts
  if ((strncmp(message,"Detected memory leaks!\n", 10) == 0)
    || ignoreMessage)
  {
    // check if the memory leak reporting ends
    if (strncmp(message,"Object dump complete.\n", 10) == 0)
    {
      _CrtSetReportHook(prevHook);
      ignoreMessage = false;
    } else
      ignoreMessage = true;

    // something from our own code?
    if(strstr(message, ".cpp") == NULL)
    {
      if(debugMsgPartsCount++ < numFollowupDebugMsgParts)
        // give it back to _CrtDbgReport() to be printed to the console
        return FALSE;
      else
        return TRUE;  // ignore it
    } else
    {
      debugMsgPartsCount = 0;
      // give it back to _CrtDbgReport() to be printed to the console
      return FALSE;
    }
  } else
    // give it back to _CrtDbgReport() to be printed to the console
    return FALSE;
}

#endif

int main(int argc, char *argv[]) {
    #if defined(_MSC_VER)
    _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
    prevHook = _CrtSetReportHook(customReportHook);
    // _CrtSetBreakAlloc(157); // Use this line to break at the nth memory allocation
    #endif

    QApplication* app = new QApplication(argc, argv);
    int appError = app->exec();
    delete app;

    #if defined(_MSC_VER)
    // Once the app has finished running and has been deleted,
    // we run this command to view the memory leaks:
    _CrtDumpMemoryLeaks();
    #endif

    return appError;
}

  1. 有了这个,您的项目现在应该能够检测到内存泄漏。请注意 _MSC_VER 定义,以便此代码仅在您从 Visual C++(而不是 Qt Creator)运行时执行。这意味着您仍然可以使用 Qt Creator 进行开发,并在需要检查内存泄漏时重新运行第 1 步。

  2. 要在特定的内存分配处中断,请使用 _CrtSetBreakAlloc() Microsoft 网站上的更多信息内存泄漏检测:http: //msdn.microsoft.com/en-us/library/e5ewb1h3%28v=vs.80 %29.aspx

原文由 laurent 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题