所以经过很多尝试,我终于找到了一种方法来检测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
#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 par