Qt Memory leaks again, Qt LTS Versions 6.2.4 and 5.15.2
-
I am just starting to develop my first Qt-application (Windows 11 host / C++ ) and I can't believe it.
Building a minimal Widget-application with Qt-Creator 8.01 and running an analysis with the build-in tool "heob", I see about 100 memory leaks!
The same problem using VS 2022 together with CRTDBG.
Is Qt such a mess? Or where is my error?
-
hi
Often such tools do not understand the Qt ownership system. -
It might be a problem with your testing methodology. For example if you use crtdbg and call
_CrtDumpMemoryLeaks()
at the end ofmain()
you'll see leaks, but that's just because there are global objects allocated that clean up after main exits.
I just created a widgets application in VS2022 and Qt 6.4 with the wizard. Setting the flags for automatic leak reporting with_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
reports no leaks for me.Can you provide example code that reports leaks for you?
-
Shure Chris, I have tree files, main.cpp, mainwindow.h and mainwindow.cpp.
First "main.cpp":
#include "mainwindow.h" #include <QApplication> #define WITH_MEM_LEAK_TESTING #ifdef WITH_MEM_LEAK_TESTING #define _CRTDBG_MAP_ALLOC #include <stdlib.h> #include <crtdbg.h> #endif int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); int ret = a.exec(); #ifdef WITH_MEM_LEAK_TESTING bool leak = _CrtDumpMemoryLeaks(); return leak; #endif return ret; }
Then "mainwindow.h":
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> QT_BEGIN_NAMESPACE namespace Ui { class MainWindow; } QT_END_NAMESPACE class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); private: Ui::MainWindow *ui; }; #endif // MAINWINDOW_H
and the last file is "mainwindow.cpp":
#include "mainwindow.h" #include "ui_mainwindow.h" MainWindow::MainWindow( QWidget* parent ) : QMainWindow( parent ) , ui( new Ui::MainWindow ) { ui->setupUi( this ); } MainWindow::~MainWindow() { delete ui; }
-
@JWSchuetz Yeah, so like I said, main is not the end of an app, so placing
_CrtDumpMemoryLeaks();
there is not gonna do what you want. Remove it and instead set automatic leak detection crt flags at the beginning of main, before you create any Qt stuff.int main(int argc, char *argv[]) { _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF); ...
This will do a dump automatically at the crt shutdown, so after everything else cleans up.
-
@Chris-Kawa First, thanks for your answer.
You are right, during the return-statement of main() some Qt-code is executed, for example the destructor of class MainWindow.
So I did as you suggested: I removed the call to
_CrtDumpMemoryLeaks()
at the end and added the call to
_CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
at the beginning of main().
But still 10 leaked memory blocks are now detected (which is much better than about 100 :-)).
I tried my two Qt LTS versions with the same result. Do you have a suggestion?
In worst case I can hope that the remaining leaks are in some static context and do not increase during the runtime of my program :-)