Làm cách nào để phát hiện rò rỉ bộ nhớ trong QtCreator trên Windows? Trên tài liệu, họ đề nghị Memcheck nhưng nó chỉ hoạt động trên Mac và Linux. Bất kỳ đề xuất cho Windows?Làm thế nào để phát hiện rò rỉ bộ nhớ trong QtCreator trên Windows?
Trả lời
Sau nhiều cố gắng cuối cùng tôi đã tìm thấy một phương pháp để phát hiện sự rò rỉ bộ nhớ của một dự án Qt trên Windows:
1) Thứ nhất, nó không thể được thực hiện trực tiếp trong Qt Creator vì vậy bạn cần tạo một Visual C++ Dự án làm phát hiện rò rỉ bộ nhớ. Rất may, qmake làm cho việc này trở nên dễ dàng. Mở công cụ dòng lệnh Qt SDK và chạy:
qmake -spec win32-msvc2008 -tp vc
Điều này sẽ chuyển đổi dự án của bạn thành tệp .vcproj.
2) Mở dự án này và thêm mã cần thiết để phát hiện rò rỉ bộ nhớ:
Để main.cpp bạn file:
// 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;
}
3) Với điều này, dự án của bạn bây giờ sẽ có thể để phát hiện rò rỉ bộ nhớ. Lưu ý rằng _MSC_VER
định nghĩa để mã này chỉ được thực hiện khi bạn chạy nó từ Visual C++ (không phải từ Qt Creator). Nó có nghĩa là bạn vẫn có thể làm việc phát triển với Qt Creator và chỉ cần chạy lại bước 1 bất cứ khi nào bạn cần kiểm tra rò rỉ bộ nhớ.
4) Để phá vỡ tại một cấp phát bộ nhớ đặc biệt, sử dụng _CrtSetBreakAlloc()
Thêm bộ nhớ thông tin phát hiện rò rỉ trên trang web của Microsoft: http://msdn.microsoft.com/en-us/library/e5ewb1h3%28v=vs.80%29.aspx
mới 2017 giải pháp
quote bởi @this.lau_
Thứ nhất, nó không thể được thực hiện trực tiếp trong Qt Creator, do đó bạn cần tạo một dự án Visual C++ để phát hiện rò rỉ bộ nhớ. Rất may, qmake làm cho việc này trở nên dễ dàng.
1) Mở công cụ dòng lệnh Qt SDK và chạy:
qmake -spec win32-msvc2015 -tp vc
2) Cài đặt Visual Leak Detector for Visual C++
3) Mở một .vcxproj đã được tạo ra với các bước 1
4) Bao gồm trong số main.cpp
#include <vld.h>
5) Launch DebugView v4.81
6) Thần chạy dự án của bạn ctrl + F5