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

lau*_*ent 14 c++ windows memory-leaks detect qt-creator

如何在Windows上的QtCreator中检测内存泄漏?在文档中,他们推荐Memcheck,但它只适用于Mac和Linux.对Windows的任何建议?

lau*_*ent 23

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

1)首先,它不能直接在Qt Creator中完成,因此您需要创建一个Visual C++项目来执行内存泄漏检测.值得庆幸的是,qmake让这很容易.打开Qt SDK命令行工具并运行:

qmake -spec win32-msvc2008 -tp vc
Run Code Online (Sandbox Code Playgroud)

这会将您的项目转换为.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;
}
Run Code Online (Sandbox Code Playgroud)

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

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


Mik*_*ail 8

2017年新解决方案

引用 @this.lau_

首先,它不能直接在Qt Creator中完成,因此您需要创建一个Visual C++项目来执行内存泄漏检测.值得庆幸的是,qmake让这很容易.

1)打开Qt SDK命令行工具并运行:

qmake -spec win32-msvc2015 -tp vc

2)安装Visual C++的Visual Leak Detector

3)打开使用该步骤创建的.vcxproj 1

4)包括在你的 main.cpp

#include <vld.h>

5)启动DebugView v4.81

6)比运行你的项目 ctrl + F5

  • 这两个答案都非常好,只是想在QT的后续版本中添加命令行`qmake -spec win32-msvc2015 -tp vc`和`qmake -tp vc`似乎可行.`-spec`参数已更改. (3认同)

Cod*_*ker 5

这是最近的答案。Qt Creator 4.7.1 现在支持 heob,它也是一个泄漏检测器。您可以从此处下载 Windows 版:“heob download | SourceForge.net”。将其提取到某处,获取最新版本的 Qt Creator,然后转到分析 | 赫布。将其定向到您的 .exe,选择您的选项,单击小磁盘图标以保存您的选项,然后单击“确定”运行您的 proggie。它提供了一个漂亮的小内存检查窗口,似乎可以在分配对象时提供堆栈转储,您可以展开并查看有问题的对象;当您使用“检测泄漏类型”选项时。您甚至可以通过单击链接导航到新发生的源代码行。

JBES 提供以下信息:

如果您使用 dwarfstack DLL 下载的 MinGW 进行编译,则可以选择下载 dwarfstack DLL 以进行正确的堆栈跟踪解析 | github.com 并将它们放在与 heob 可执行文件相同的文件夹中。

  • 我发现 Heob 的输出不太有用,即使是最简单的应用程序(包括 Qt 的演示应用程序),它也会检测到 100 个错误泄漏。 (2认同)