When QDialog close, Memery leak!



  • In MainWindow create a dialog and show,after use,close it.Memory is not fully released.

    The create code as follow:
    mTestDialog=new TestDialog(this);
    mTestDialog->setAttribute(Qt::WA_DeleteOnClose);
    mTestDialog->show();

    The after use code:
    close();

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

    Memery status of the dialog create:

    0_1504690083478_微信图片_20170906172455.png

    Memery status of the dialog close:

    0_1504690101455_微信图片_20170906172511.png

    [Moved to General and Desktop ~kshegunov]


  • Lifetime Qt Champion

    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.



  • 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]



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



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



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


  • Lifetime Qt Champion

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



  • Did you reimplement TestDialog::closeEvent?



  • @VRonin

    I didn't reimplement TestDialog::closeEvent?



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

  • Lifetime Qt Champion

    What is this code supposed to do ?

    It looks like a really convoluted key press handler.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.