Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Qt Memory leaks again, Qt LTS Versions 6.2.4 and 5.15.2
Forum Updated to NodeBB v4.3 + New Features

Qt Memory leaks again, Qt LTS Versions 6.2.4 and 5.15.2

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 3 Posters 1.9k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    JWSchuetz
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      hi
      Often such tools do not understand the Qt ownership system.

      https://doc.qt.io/qt-6/objecttrees.html

      1 Reply Last reply
      1
      • Chris KawaC Online
        Chris KawaC Online
        Chris Kawa
        Lifetime Qt Champion
        wrote on last edited by
        #3

        It might be a problem with your testing methodology. For example if you use crtdbg and call _CrtDumpMemoryLeaks() at the end of main() 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?

        1 Reply Last reply
        3
        • J Offline
          J Offline
          JWSchuetz
          wrote on last edited by
          #4

          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;
          }
          
          1 Reply Last reply
          0
          • J Offline
            J Offline
            JWSchuetz
            wrote on last edited by
            #5
            This post is deleted!
            1 Reply Last reply
            0
            • Chris KawaC Online
              Chris KawaC Online
              Chris Kawa
              Lifetime Qt Champion
              wrote on last edited by Chris Kawa
              #6

              @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.

              J 1 Reply Last reply
              5
              • J Offline
                J Offline
                JWSchuetz
                wrote on last edited by
                #7
                This post is deleted!
                1 Reply Last reply
                0
                • Chris KawaC Chris Kawa

                  @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.

                  J Offline
                  J Offline
                  JWSchuetz
                  wrote on last edited by
                  #8

                  @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 :-)

                  1 Reply Last reply
                  0
                  • J Offline
                    J Offline
                    JWSchuetz
                    wrote on last edited by
                    #9

                    I found out that ignoring "block type identifiers" using only

                        _CrtSetDbgFlag( _CRTDBG_LEAK_CHECK_DF );
                    
                    

                    lead to zero leaked blocks. For me that's good enough. Thanks to all responders.

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved