How to detect memory leaks in QtCreator on Windows?(如何在 Windows 上的 QtCreator 中检测内存泄漏?)
问题描述
如何在 Windows 上的 QtCreator 中检测内存泄漏?在文档上,他们推荐 Memcheck,但它仅适用于 Mac 和 Linux.对 Windows 有什么建议吗?
How can I detect memory leaks in QtCreator on Windows? On the doc, they recommend Memcheck but it only works on Mac and Linux. Any suggestion for Windows?
推荐答案
经过多次尝试,我终于找到了一种在 Windows 上检测 Qt 项目内存泄漏的方法:
After many tries I finally found a method to detect the memory leaks of a Qt project on Windows:
1) 首先,它不能直接在Qt Creator中完成,因此您需要创建一个Visual C++项目来进行内存泄漏检测.值得庆幸的是,qmake 使这变得容易.打开Qt SDK命令行工具,运行:
1) First, it cannot be done directly in Qt Creator so you need to create a Visual C++ project to do the memory leak detection. Thankfully, qmake makes this easy. Open the Qt SDK command line tool and run:
这会将您的项目转换为 .vcproj.
This will convert your project to a .vcproj.
2) 打开这个项目并添加必要的内存泄漏检测代码:
2) Open this project and add the necessary code for memory leak detection:
到您的 main.cpp 文件:
3) 有了这个,您的项目现在应该能够检测内存泄漏.请注意 _MSC_VER
定义,只有当您从 Visual C++(而不是从 Qt Creator)运行此代码时才会执行此代码.这意味着您仍然可以使用 Qt Creator 进行开发,只需在需要检查内存泄漏时重新运行步骤 1.
3) With this, your project should now be able to detect memory leaks. Note the _MSC_VER
defines so that this code is only executed when your run it from Visual C++ (not from Qt Creator). It means you can still do the development with Qt Creator and just re-run step 1 whenever you need to check for memory leaks.
4) 要中断特定的内存分配,请使用 _CrtSetBreakAlloc()
Microsoft 网站上的更多信息内存泄漏检测:http://msdn.microsoft.com/en-us/library/e5ewb1h3%28v=vs.80%29.aspx
4) To break at a particular memory allocation, use _CrtSetBreakAlloc()
More information memory leak detection on Microsoft's website: http://msdn.microsoft.com/en-us/library/e5ewb1h3%28v=vs.80%29.aspx
这篇关于如何在 Windows 上的 QtCreator 中检测内存泄漏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!