Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. 3rd Party Software
  4. Memory leaks in Qt app
Qt 6.11 is out! See what's new in the release blog

Memory leaks in Qt app

Scheduled Pinned Locked Moved Unsolved 3rd Party Software
15 Posts 2 Posters 8.1k Views 1 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.
  • ? A Former User

    Hi! Are these leaks in your code or in code from the Qt framework?

    Cobra91151C Offline
    Cobra91151C Offline
    Cobra91151
    wrote on last edited by
    #3

    @Wieland

    Some in my code and some in qt:
    VLD output:

    Qt5Cored.dll!QPauseAnimation::duration() + 0x44B42D bytes
    Qt5Cored.dll!QPauseAnimation::duration() + 0x456349 bytes
    Qt5Cored.dll!QPauseAnimation::duration() + 0x44CC79 bytes
    Qt5Cored.dll!QPauseAnimation::duration() + 0x505C71 bytes
    qwindowsd.dll!qt_plugin_instance() + 0xD7661 bytes
    Qt5Cored.dll!QPauseAnimation::duration() + 0x503988 bytes
    USER32.dll!CallWindowProcW() + 0x4F4 bytes
    USER32.dll!DispatchMessageW() + 0x1BC bytes
    Qt5Cored.dll!QPauseAnimation::duration() + 0x504392 bytes
    qwindowsd.dll!qt_plugin_instance() + 0xD7621 bytes
    Qt5Cored.dll!QPauseAnimation::duration() + 0x44607F bytes
    Qt5Cored.dll!QPauseAnimation::duration() + 0x4462C5 bytes
    Qt5Cored.dll!QPauseAnimation::duration() + 0x449196 bytes
    Qt5Guid.dll!QOpenGLFunctions_1_1::glVertex2iv() + 0x99E9D bytes
    
    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #4

      I'd say fix your bugs first and then look for known issues / report issues at https://bugreports.qt.io.

      Cobra91151C 1 Reply Last reply
      0
      • ? A Former User

        I'd say fix your bugs first and then look for known issues / report issues at https://bugreports.qt.io.

        Cobra91151C Offline
        Cobra91151C Offline
        Cobra91151
        wrote on last edited by
        #5

        @Wieland

        Ok. But how to fix this:

        c:\..\..\qt projects\test\main.cpp (16): Test.exe!main() + 0x6 bytes

        On 16 line is return app.exec(); function?

        1 Reply Last reply
        0
        • ? Offline
          ? Offline
          A Former User
          wrote on last edited by
          #6

          You need to check the body of your main() function for heap allocations / non-deleted objects. If you like, post it here, so we can try to find the problem together.

          Cobra91151C 1 Reply Last reply
          1
          • ? A Former User

            You need to check the body of your main() function for heap allocations / non-deleted objects. If you like, post it here, so we can try to find the problem together.

            Cobra91151C Offline
            Cobra91151C Offline
            Cobra91151
            wrote on last edited by Cobra91151
            #7

            @Wieland

            ManageStartup *appWindow = new ManageStartup();
            appWindow->setParent(nullptr);
            return app.exec();
            

            ManageStartup - QObject class, in this class I check some data before displaying the main GUI window.

            Also I have noticed that Qt 5.7.1 in debug mode when debugging app takes more memory than in release. In Qt 5.7.0 memory management was the same in debug and release modes.

            1 Reply Last reply
            0
            • ? Offline
              ? Offline
              A Former User
              wrote on last edited by A Former User
              #8

              And that's a bug. You allocate appWindow on the heap. As it is a QObject, you could do the following:

              appWindow->setParent(&app);
              

              In this case, once app gets destroyed (when control leaves main), app would automatically delete its child `` appWindow```.

              But right now, appWindow has no QObject-parent, so you have to delete it by yourself.

              Or just make it a stack object:

              ManageStartup appWindow;
              
              Cobra91151C 1 Reply Last reply
              2
              • ? A Former User

                And that's a bug. You allocate appWindow on the heap. As it is a QObject, you could do the following:

                appWindow->setParent(&app);
                

                In this case, once app gets destroyed (when control leaves main), app would automatically delete its child `` appWindow```.

                But right now, appWindow has no QObject-parent, so you have to delete it by yourself.

                Or just make it a stack object:

                ManageStartup appWindow;
                
                Cobra91151C Offline
                Cobra91151C Offline
                Cobra91151
                wrote on last edited by Cobra91151
                #9

                @Wieland

                Thanks. I have changed code to:

                ManageStartup appWindow;
                appWindow.setParent(&app);
                return app.exec();
                

                and check for memory leaks again, but the leak is still in main.cpp (16): Test.exe!main() + 0x6 bytes, return app.exec();

                Main.cpp:

                #include <QApplication>
                #include <vld.h>
                #include "managestartup.h"
                
                int main(int argc, char *argv[])
                {
                    QApplication app(argc, argv);
                    QThread::currentThread()->setPriority(QThread::HighestPriority);
                    QSharedMemory sharedMemory(APP_NAME);
                    app.setApplicationVersion(APP_VERSION);
                
                    if (sharedMemory.create(1) && sharedMemory.error() != QSharedMemory::AlreadyExists) {
                        ManageStartup appWindow;
                        appWindow.setParent(&app);
                        return app.exec();
                    } else {
                       int userRespond = QMessageBox::warning(nullptr, QObject::tr("Test"), QObject::tr("Another instance?"), QMessageBox::Yes | QMessageBox::No);
                
                       if (userRespond == QMessageBox::Yes) {
                           ManageStartup appWindow;
                           appWindow.setParent(&app);
                           return app.exec();
                       }
                    }    
                }
                
                1 Reply Last reply
                0
                • ? Offline
                  ? Offline
                  A Former User
                  wrote on last edited by
                  #10

                  That looks good to me so far. Now follow the call stack in the VLD log. I guess the next leak is in ManageStartup.

                  Cobra91151C 2 Replies Last reply
                  2
                  • ? A Former User

                    That looks good to me so far. Now follow the call stack in the VLD log. I guess the next leak is in ManageStartup.

                    Cobra91151C Offline
                    Cobra91151C Offline
                    Cobra91151
                    wrote on last edited by
                    #11

                    @Wieland

                    Ok. I will check it. Thanks.

                    1 Reply Last reply
                    0
                    • ? A Former User

                      That looks good to me so far. Now follow the call stack in the VLD log. I guess the next leak is in ManageStartup.

                      Cobra91151C Offline
                      Cobra91151C Offline
                      Cobra91151
                      wrote on last edited by
                      #12

                      @Wieland

                      Hi! I have fixed some memory leaks by changing objects from heap memory allocation to stack but some leaks still exists.

                      c:..\qt projects\test\test.cpp (2885): Test.exe!Test::event()

                      bool Test::event(QEvent *event)
                      {
                          if (event->type() == QEvent::WindowStateChange) {
                              appSettings.beginGroup("AppSettings");
                      
                              if (this->isMinimized() && appSettings.allKeys().contains("minimizeToTray") && appSettings.value("minimizeToTray") == true) {
                                  setNotifyIcon();
                                  showNotification();
                                  this->hide();
                              }
                      
                              appSettings.endGroup();
                          }
                      
                          return QWidget::event(event);
                      }
                      

                      On line 2885 is } so I think something with return QWidget::event(event)? Thanks in advance.

                      1 Reply Last reply
                      0
                      • ? Offline
                        ? Offline
                        A Former User
                        wrote on last edited by A Former User
                        #13

                        The reported potential leak regarding event is a false positive, just ignore it. VLD simply isn't smart enough to understand what's going on.

                        See also QCoreApplication Class:

                        the post event queue will take ownership of the event and delete it once it has been posted

                        Cobra91151C 1 Reply Last reply
                        3
                        • ? A Former User

                          The reported potential leak regarding event is a false positive, just ignore it. VLD simply isn't smart enough to understand what's going on.

                          See also QCoreApplication Class:

                          the post event queue will take ownership of the event and delete it once it has been posted

                          Cobra91151C Offline
                          Cobra91151C Offline
                          Cobra91151
                          wrote on last edited by
                          #14

                          @Wieland

                          Ok. Thanks.

                          1 Reply Last reply
                          0
                          • Cobra91151C Offline
                            Cobra91151C Offline
                            Cobra91151
                            wrote on last edited by Cobra91151
                            #15

                            My app gets WMI data and displays it as QTreeWidgetItem, but it consumes to much RAM memory when changing languages.

                            void MyApp::appOSThread()
                            {
                               try {
                                    osWorker = new Worker();
                                    osThread = new QThread(osWorker);
                                    osWorker->moveToThread(osThread);
                                    connect(osThread, &QThread::started, osWorker, &Worker::appOSWMIData);
                                    connect(osWorker, &Worker::appOSData, this, &MyApp::osWMIData);
                                    connect(osWorker, &Worker::hardwareDataNotAvailable, this, &MyApp::osWMIDataNotAvailable);
                                    connect(osWorker, &Worker::errorNotSupportedOS, this, &MyApp::appNotSupportedOS);
                                    connect(osWorker, &Worker::finished, osThread, &QThread::quit, Qt::DirectConnection);
                                    connect(osThread, &QThread::destroyed, osThread, &QThread::deleteLater);
                                    osThread->start();
                                    logData(QObject::tr("Application: gets OS information"));
                                } catch (...) {
                                    QMessageBox::critical(this, QObject::tr("Error"), QObject::tr("An error has occurred with detecting OS!"));
                                }
                            }
                            
                            void MyApp::osWMIData(QStringList property, QStringList data)
                            {
                                int countOSProperties = property.count();
                                QString osProperty;
                                QString osData;
                            
                                for (int i = 0; i < countOSProperties; i++) {
                                    osProperty = property.at(i);
                                    osData = data.at(i);
                                    osItem = new QTreeWidgetItem(osView); //osView - QTreeWidget
                                    osItem->setText(0, osProperty);
                                    osItem->setText(1, osData);
                                }
                            }
                            
                            void MyApp::appLocalization()
                            {
                                osView->clear();
                                appOSThread();
                            }
                            

                            For example when app is started takes 30 MB when change language then it takes 3 - 5 MB RAM and when changing again it takes again 5 - 10 MB of RAM.

                            Any ideas?

                            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