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. When QDialog close, Memery leak!
Qt 6.11 is out! See what's new in the release blog

When QDialog close, Memery leak!

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 4 Posters 3.7k 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.
  • SGaistS Offline
    SGaistS Offline
    SGaist
    Lifetime Qt Champion
    wrote on last edited by
    #2

    Hi and welcome to devnet,

    Deleting an object allocated on the heap doesn't mean the memory used for it will be immediately returned to the system.

    Your test also doesn't take into account all the processes currently running on your system.

    Interested in AI ? www.idiap.ch
    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

    R 1 Reply Last reply
    0
    • R Offline
      R Offline
      Roy.Wang
      wrote on last edited by kshegunov
      #3

      Why create a dialog use 7.8M memery?

      The UI is simple, with only four label,the size is 1280*720

      testdialog.ui

      <?xml version="1.0" encoding="UTF-8"?>
      <ui version="4.0">
       <class>TestDialog</class>
       <widget class="QDialog" name="TestDialog">
        <property name="geometry">
         <rect>
          <x>0</x>
          <y>0</y>
          <width>1280</width>
          <height>720</height>
         </rect>
        </property>
        <property name="windowTitle">
         <string>Dialog</string>
        </property>
        <property name="styleSheet">
         <string notr="true">background-color: rgb(255, 255, 255);</string>
        </property>
        <widget class="QLabel" name="label">
         <property name="geometry">
          <rect>
           <x>250</x>
           <y>260</y>
           <width>161</width>
           <height>61</height>
          </rect>
         </property>
         <property name="styleSheet">
          <string notr="true">background-color: rgb(0, 255, 127);</string>
         </property>
         <property name="text">
          <string>Label1</string>
         </property>
         <property name="alignment">
          <set>Qt::AlignCenter</set>
         </property>
        </widget>
        <widget class="QLabel" name="label_2">
         <property name="geometry">
          <rect>
           <x>250</x>
           <y>430</y>
           <width>161</width>
           <height>61</height>
          </rect>
         </property>
         <property name="styleSheet">
          <string notr="true">background-color: rgb(85, 255, 0);</string>
         </property>
         <property name="text">
          <string>TextLabel</string>
         </property>
        </widget>
        <widget class="QLabel" name="label_3">
         <property name="geometry">
          <rect>
           <x>590</x>
           <y>270</y>
           <width>161</width>
           <height>61</height>
          </rect>
         </property>
         <property name="styleSheet">
          <string notr="true">background-color: rgb(0, 170, 0);</string>
         </property>
         <property name="text">
          <string>TextLabel</string>
         </property>
        </widget>
        <widget class="QLabel" name="label_4">
         <property name="geometry">
          <rect>
           <x>580</x>
           <y>410</y>
           <width>161</width>
           <height>61</height>
          </rect>
         </property>
         <property name="styleSheet">
          <string notr="true">background-color: rgb(85, 170, 0);</string>
         </property>
         <property name="text">
          <string>TextLabel</string>
         </property>
        </widget>
       </widget>
       <resources/>
       <connections/>
      </ui>
      

      [Added code tags ~kshegunov]

      J.HilkJ 1 Reply Last reply
      0
      • R Roy.Wang

        Why create a dialog use 7.8M memery?

        The UI is simple, with only four label,the size is 1280*720

        testdialog.ui

        <?xml version="1.0" encoding="UTF-8"?>
        <ui version="4.0">
         <class>TestDialog</class>
         <widget class="QDialog" name="TestDialog">
          <property name="geometry">
           <rect>
            <x>0</x>
            <y>0</y>
            <width>1280</width>
            <height>720</height>
           </rect>
          </property>
          <property name="windowTitle">
           <string>Dialog</string>
          </property>
          <property name="styleSheet">
           <string notr="true">background-color: rgb(255, 255, 255);</string>
          </property>
          <widget class="QLabel" name="label">
           <property name="geometry">
            <rect>
             <x>250</x>
             <y>260</y>
             <width>161</width>
             <height>61</height>
            </rect>
           </property>
           <property name="styleSheet">
            <string notr="true">background-color: rgb(0, 255, 127);</string>
           </property>
           <property name="text">
            <string>Label1</string>
           </property>
           <property name="alignment">
            <set>Qt::AlignCenter</set>
           </property>
          </widget>
          <widget class="QLabel" name="label_2">
           <property name="geometry">
            <rect>
             <x>250</x>
             <y>430</y>
             <width>161</width>
             <height>61</height>
            </rect>
           </property>
           <property name="styleSheet">
            <string notr="true">background-color: rgb(85, 255, 0);</string>
           </property>
           <property name="text">
            <string>TextLabel</string>
           </property>
          </widget>
          <widget class="QLabel" name="label_3">
           <property name="geometry">
            <rect>
             <x>590</x>
             <y>270</y>
             <width>161</width>
             <height>61</height>
            </rect>
           </property>
           <property name="styleSheet">
            <string notr="true">background-color: rgb(0, 170, 0);</string>
           </property>
           <property name="text">
            <string>TextLabel</string>
           </property>
          </widget>
          <widget class="QLabel" name="label_4">
           <property name="geometry">
            <rect>
             <x>580</x>
             <y>410</y>
             <width>161</width>
             <height>61</height>
            </rect>
           </property>
           <property name="styleSheet">
            <string notr="true">background-color: rgb(85, 170, 0);</string>
           </property>
           <property name="text">
            <string>TextLabel</string>
           </property>
          </widget>
         </widget>
         <resources/>
         <connections/>
        </ui>
        

        [Added code tags ~kshegunov]

        J.HilkJ Offline
        J.HilkJ Offline
        J.Hilk
        Moderators
        wrote on last edited by
        #4

        @Roy.Wang
        my Guess is, you're running in Debug mode, try the release one, should reduce the memory significantly


        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        R 1 Reply Last reply
        1
        • SGaistS SGaist

          Hi and welcome to devnet,

          Deleting an object allocated on the heap doesn't mean the memory used for it will be immediately returned to the system.

          Your test also doesn't take into account all the processes currently running on your system.

          R Offline
          R Offline
          Roy.Wang
          wrote on last edited by
          #5

          @SGaist
          You said the memory will not be released immediately, but it will not be released for a long time.

          Create ,then close,Run several times, increasing memory usage until the process is killed!

          No other processes are operating

          1 Reply Last reply
          1
          • J.HilkJ J.Hilk

            @Roy.Wang
            my Guess is, you're running in Debug mode, try the release one, should reduce the memory significantly

            R Offline
            R Offline
            Roy.Wang
            wrote on last edited by
            #6

            @J.Hilk
            It doesn't matter with that, it's still that big

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #7

              Can you show the complete code you use that shows that behaviour ?

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              R 1 Reply Last reply
              1
              • VRoninV Offline
                VRoninV Offline
                VRonin
                wrote on last edited by
                #8

                Did you reimplement TestDialog::closeEvent?

                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                ~Napoleon Bonaparte

                On a crusade to banish setIndexWidget() from the holy land of Qt

                R 1 Reply Last reply
                0
                • VRoninV VRonin

                  Did you reimplement TestDialog::closeEvent?

                  R Offline
                  R Offline
                  Roy.Wang
                  wrote on last edited by
                  #9

                  @VRonin

                  I didn't reimplement TestDialog::closeEvent?

                  1 Reply Last reply
                  0
                  • SGaistS SGaist

                    Can you show the complete code you use that shows that behaviour ?

                    R Offline
                    R Offline
                    Roy.Wang
                    wrote on last edited by VRonin
                    #10

                    @SGaist @VRonin

                    The complete code as follow:

                    main.cpp

                    #include "mainwindow.h"
                    #include <QApplication>
                    int main(int argc, char *argv[])
                    {
                        QApplication a(argc, argv);
                        MainWindow w;
                        w.show();
                        return a.exec();
                    }
                    

                    mainwindow.h

                    #ifndef MAINWINDOW_H
                    #define MAINWINDOW_H
                    
                    #include <QMainWindow>
                    #include "icameracallback.h"
                    #include "testdialog.h"
                    
                    namespace Ui {
                    class MainWindow;
                    }
                    
                    class MainWindow : public QMainWindow,public ICameraCallback
                    {
                        Q_OBJECT
                    
                    public:
                        explicit MainWindow(QWidget *parent = 0);
                        ~MainWindow();
                        virtual void onKeyEvent(int id);
                        TestDialog *mTestDialog;
                    
                    private:
                        Ui::MainWindow *ui;
                    
                    
                    signals:
                    void keyChanged(int);
                    public slots:
                    void updateUI(int);
                    };
                    
                    #endif // MAINWINDOW_H
                    

                    mainwindow.cpp

                    #include "mainwindow.h"
                    #include "ui_mainwindow.h"
                    #include "keyevent.h"
                    
                    KeyEventThread *mKeyEvent;
                    MainWindow *mMainWindow;
                    MainWindow::MainWindow(QWidget *parent) :
                        QMainWindow(parent),
                        ui(new Ui::MainWindow)
                    {
                        ui->setupUi(this);
                        mMainWindow=this;
                        connect(this,SIGNAL(keyChanged(int)),this,SLOT(updateUI(int)));
                        mKeyEvent=new KeyEventThread(this);
                        mKeyEvent->setEventCallBack(this);
                        mKeyEvent->start();
                    }
                    
                    MainWindow::~MainWindow()
                    {
                        delete ui;
                    }
                    
                    void MainWindow::onKeyEvent(int value)
                    {
                        emit keyChanged(value);
                    }
                    void MainWindow::updateUI(int id){
                        printf("MainWindowImpl::onKeyEvent id =%d \n",id);
                        switch (id) {
                        case EVENT_KEY_CODE_MENU:
                        case EVENT_KEY_CODE_ENTER:
                            mTestDialog=new TestDialog(this);
                            mTestDialog->setAttribute(Qt::WA_DeleteOnClose);
                            mKeyEvent->setEventCallBack(mTestDialog);
                            mTestDialog->show();
                            break;
                        default:
                            break;
                        }
                    }
                    

                    testdialog.h

                    #ifndef TESTDIALOG_H
                    #define TESTDIALOG_H
                    
                    #include <QDialog>
                    #include "icameracallback.h"
                    
                    namespace Ui {
                    class TestDialog;
                    }
                    
                    class TestDialog : public QDialog,public ICameraCallback
                    {
                        Q_OBJECT
                    
                    public:
                        explicit TestDialog(QWidget *parent = 0);
                        ~TestDialog();
                         virtual void onKeyEvent(int id);
                    
                    private:
                           Ui::TestDialog *ui;
                    
                    signals:
                        void keyChanged(int);
                    public slots:
                        void updateUI(int);
                    };
                    

                    #endif // TESTDIALOG_H

                    testdialog.cpp

                    #include "testdialog.h"
                    #include "ui_testdialog.h"
                    #include "keyevent.h"
                    #include "mainwindow.h"
                    extern KeyEventThread *mKeyEvent;
                    extern MainWindow *mMainWindow;
                    TestDialog::TestDialog(QWidget *parent) :
                        QDialog(parent),
                        ui(new Ui::TestDialog)
                    {
                        ui->setupUi(this);
                        connect(this,SIGNAL(keyChanged(int)),this,SLOT(updateUI(int)));
                        printf("------------TestDialog::Create()----------\n");
                    }
                    
                    void TestDialog::onKeyEvent(int value)
                    {
                        emit keyChanged(value);
                    }
                    
                    TestDialog::~TestDialog()
                    {
                        delete ui;
                        printf("------------TestDialog::~TestDialog()----------\n");
                    }
                    void TestDialog::updateUI(int id){
                        printf("TestDialog::onKeyEvent id =%d \n",id);
                        switch (id) {
                        case EVENT_KEY_CODE_BACK:
                            mKeyEvent->setEventCallBack(mMainWindow);
                            close();
                            break;
                        default:
                            break;
                        }
                    }
                    ``
                    
                    **keyevent.h**
                    ```cpp
                    #ifndef KEYEVENT_H
                    #define KEYEVENT_H
                    
                    #include <QObject>
                    #include<QThread>
                    #include<QtCore>
                    #include <stdio.h>
                    #include "icameracallback.h"
                    #define EVENT_KEY_NAME  "/dev/input/event4"
                    //KEY CODE
                    #define EVENT_KEY_CODE_ONE 2
                    #define EVENT_KEY_CODE_TWO 3
                    #define EVENT_KEY_CODE_THREE 4
                    #define EVENT_KEY_CODE_FOUR 5
                    #define EVENT_KEY_CODE_FIVE 6
                    #define EVENT_KEY_CODE_SIX 7
                    #define EVENT_KEY_CODE_SEVEN 8
                    #define EVENT_KEY_CODE_EIGHT 9
                    #define EVENT_KEY_CODE_NINE 10
                    #define EVENT_KEY_CODE_ZERO 11
                    #define EVENT_KEY_CODE_ENTER 28
                    #define EVENT_KEY_CODE_SAVE 60
                    #define EVENT_KEY_CODE_UP 103
                    #define EVENT_KEY_CODE_LEFT 105
                    #define EVENT_KEY_CODE_RIGHT 106
                    #define EVENT_KEY_CODE_DOWN 108
                    #define EVENT_KEY_CODE_MENU 139
                    #define EVENT_KEY_CODE_BACK 158
                    //KEY VALUE
                    #define EVENT_KEY_VALUE_DOWN 1
                    #define EVENT_KEY_VALUE_UP 0
                    
                    
                    
                    class KeyEventThread : public QThread
                    {
                        Q_OBJECT
                    public:
                       explicit KeyEventThread(QObject *parent = 0);
                        ~KeyEventThread();
                        void run();
                        void setEventCallBack(ICameraCallback* pICameraCallback);
                    
                    
                    private:
                        ICameraCallback* mICameraCallback;
                    };
                    
                    #endif // KEYEVENT_H
                    

                    keyevent.cpp

                    #include "keyevent.h"
                    #include <stdio.h>
                    #include <sys/types.h>
                    #include <linux/input.h>
                    #include <fcntl.h>
                    #include <time.h>
                    
                    KeyEventThread::KeyEventThread(QObject *parent)
                    {}
                    KeyEventThread::~KeyEventThread()
                    {}
                    void KeyEventThread::run()
                    {
                        int fd=open(EVENT_KEY_NAME,O_RDONLY);
                        while(fd==-1)
                            {
                            printf("open /dev/input/event4 fail!!!!!!!!!\n");
                            sleep(1);
                            fd=open(EVENT_KEY_NAME,O_RDONLY);
                        }
                        while(1)
                        {
                                int n;
                                struct input_event t;
                                if(read(fd, &t, sizeof (t))>0){
                                  if(NULL!=mICameraCallback&&t.value==EVENT_KEY_VALUE_DOWN){
                                        printf ("key event===========type=%d code=%d value=%d\n", t.type,t.code,t.value);
                                        mICameraCallback->onKeyEvent(t.code);
                                  }
                                }
                        }
                    
                         printf("KeyEventThread stop!!!!!!!!!\n");
                    }
                    
                    void KeyEventThread::setEventCallBack(ICameraCallback* pICameraCallback)
                    {
                        mICameraCallback=pICameraCallback;
                    }
                    

                    icameracallback.h

                    #ifndef ICAMERACALLBACK_H
                    #define ICAMERACALLBACK_H
                    #include <stdio.h>
                    
                    class ICameraCallback
                    {
                    public:
                        ICameraCallback();
                        virtual void onKeyEvent(int id);
                    };
                    
                    #endif // ICAMERACALLBACK_H
                    

                    icameracallback.cpp

                    #include "icameracallback.h"
                    ICameraCallback::ICameraCallback()
                    {
                    
                    }
                    
                    void ICameraCallback::onKeyEvent(int id)
                    {
                              printf("ICameraCallback::onKeyEvent id =%d \n",id);
                    }
                    

                    mainwindow.ui

                    <?xml version="1.0" encoding="UTF-8"?>
                    <ui version="4.0">
                     <class>MainWindow</class>
                     <widget class="QMainWindow" name="MainWindow">
                      <property name="geometry">
                       <rect>
                        <x>0</x>
                        <y>0</y>
                        <width>1280</width>
                        <height>720</height>
                       </rect>
                      </property>
                      <property name="windowTitle">
                       <string>MainWindow</string>
                      </property>
                      <property name="styleSheet">
                       <string notr="true">background-color: rgb(170, 255, 255);</string>
                      </property>
                      <widget class="QWidget" name="centralWidget">
                       <widget class="QLabel" name="label">
                        <property name="geometry">
                         <rect>
                          <x>550</x>
                          <y>250</y>
                          <width>141</width>
                          <height>51</height>
                         </rect>
                        </property>
                        <property name="styleSheet">
                         <string notr="true">background-color: rgb(0, 170, 0);</string>
                        </property>
                        <property name="text">
                         <string>Test</string>
                        </property>
                        <property name="alignment">
                         <set>Qt::AlignCenter</set>
                        </property>
                       </widget>
                      </widget>
                     </widget>
                     <layoutdefault spacing="6" margin="11"/>
                     <resources/>
                     <connections/>
                    </ui>
                    

                    testdialog.ui

                    <?xml version="1.0" encoding="UTF-8"?>
                    <ui version="4.0">
                     <class>TestDialog</class>
                     <widget class="QDialog" name="TestDialog">
                      <property name="geometry">
                       <rect>
                        <x>0</x>
                        <y>0</y>
                        <width>1280</width>
                        <height>720</height>
                       </rect>
                      </property>
                      <property name="windowTitle">
                       <string>Dialog</string>
                      </property>
                      <property name="styleSheet">
                       <string notr="true">background-color: rgb(255, 255, 255);</string>
                      </property>
                      <widget class="QLabel" name="label">
                       <property name="geometry">
                        <rect>
                         <x>10</x>
                         <y>20</y>
                         <width>161</width>
                         <height>61</height>
                        </rect>
                       </property>
                       <property name="styleSheet">
                        <string notr="true">background-color: rgb(0, 255, 127);</string>
                       </property>
                       <property name="text">
                        <string>Label1</string>
                       </property>
                       <property name="alignment">
                        <set>Qt::AlignCenter</set>
                       </property>
                      </widget>
                      <widget class="QLabel" name="label_2">
                       <property name="geometry">
                        <rect>
                         <x>10</x>
                         <y>120</y>
                         <width>161</width>
                         <height>61</height>
                        </rect>
                       </property>
                       <property name="styleSheet">
                        <string notr="true">background-color: rgb(85, 255, 0);</string>
                       </property>
                       <property name="text">
                        <string>TextLabel</string>
                       </property>
                      </widget>
                      <widget class="QLabel" name="label_3">
                       <property name="geometry">
                        <rect>
                         <x>190</x>
                         <y>20</y>
                         <width>161</width>
                         <height>61</height>
                        </rect>
                       </property>
                       <property name="styleSheet">
                        <string notr="true">background-color: rgb(0, 170, 0);</string>
                       </property>
                       <property name="text">
                        <string>TextLabel</string>
                       </property>
                      </widget>
                      <widget class="QLabel" name="label_4">
                       <property name="geometry">
                        <rect>
                         <x>190</x>
                         <y>120</y>
                         <width>161</width>
                         <height>61</height>
                        </rect>
                       </property>
                       <property name="styleSheet">
                        <string notr="true">background-color: rgb(85, 170, 0);</string>
                       </property>
                       <property name="text">
                        <string>TextLabel</string>
                       </property>
                      </widget>
                     </widget>
                     <resources/>
                     <connections/>
                    </ui>
                    
                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #11

                      What is this code supposed to do ?

                      It looks like a really convoluted key press handler.

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      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