Results 1 to 8 of 8

Thread: How to correctly delete a QDialog created via 'new'?

  1. #1
    Join Date
    Oct 2009
    Posts
    36
    Thanks
    5
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default How to correctly delete a QDialog created via 'new'?

    Hello.

    I have an app that can have a number of info windows. I create them using:
    Qt Code:
    1. CInfoWnd* wnd = new CInfoWnd( main_wnd );
    2. wnd->show();
    To copy to clipboard, switch view to plain text mode 

    But since they are automatically deleted only after they parent object is deleted (main window), they remain in meory even after user closes them. Si, if user constantly opens and closes info windows, memory is constantly leaking. What is a preffered way in Qt to dispose windows that are created via 'new'?

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: How to correctly delete a QDialog created via 'new'?

    You can reuse the window instead creating a new one. If you what delete it use
    Qt Code:
    1. delete wnd;
    2. //or
    3. wnd.deleteLater();
    To copy to clipboard, switch view to plain text mode 
    . It does not matter. You also can use the window attribute deleteOnClose.

  3. The following user says thank you to Lykurg for this useful post:

    eyeofhell (31st May 2010)

  4. #3
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to correctly delete a QDialog created via 'new'?

    Qt Code:
    1. CInfoWnd* wnd = new CInfoWnd( main_wnd );
    2. wnd->setAttribute( Qt::WA_DeleteOnClose, true );
    3. wnd->show();
    To copy to clipboard, switch view to plain text mode 
    Dialog will be deleted after close.

  5. #4
    Join Date
    Oct 2009
    Posts
    36
    Thanks
    5
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to correctly delete a QDialog created via 'new'?

    Dialog will be deleted after close.
    Unfortunately, no. At last for Qt 4.6. I have tested this by opening and closing a dialog - the memory is constantly incrementing by 500kb per dialog .

  6. #5
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to correctly delete a QDialog created via 'new'?

    Can you share the code ??
    Without it, hard to say..

  7. #6
    Join Date
    Oct 2009
    Posts
    36
    Thanks
    5
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to correctly delete a QDialog created via 'new'?

    I have checked all mentioned ways. Overriding eventClose() and issuing deleteLater() in it works like a charm.
    deleteOnClose attribute works for empty QDialog, but after adding child widgets, signals and slots app crash with access violation on internal delete upon dialog close. Maybe this flag has some additional constraits / conditions i'm not aware about.

  8. #7
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: How to correctly delete a QDialog created via 'new'?

    but after adding child widgets, signals and slots app crash with access violation on internal delete upon dialog close.
    Any QObject which you allocate on the heap, and do not parent, you have to delete your self.
    This could cause such an error, when some objects are deleted, but not all, and/or, when the deletion is not in the correct order, and/or when you have parented the objects, and still try to delete them your self after they have been deleted by the parent.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  9. #8
    Join Date
    Nov 2010
    Posts
    20
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to correctly delete a QDialog created via 'new'?

    *Moderate dig*

    The problem with calling deleteLater() on the QDialog or deleting it after exec() is, that for a short moment the dialog will freeze and then disappear when clicking a button that causes the function to return, instead of just disappearing normally (or fade-away animation under Vista/W7). That simply is not nice.

    I have made a dirty workaround which does not take the lag from the main event loop, but at least lets the animation play smoothly:

    dialogdestroyer.h:
    Qt Code:
    1. #ifndef DIALOGDESTROYER_H
    2. #define DIALOGDESTROYER_H
    3.  
    4. #include <QThread>
    5. #include <QDialog>
    6.  
    7. class DialogDestroyer : public QThread
    8. {
    9. public:
    10. DialogDestroyer();
    11.  
    12. void DelayedDestruction(QDialog* lDialog);
    13.  
    14. private:
    15. void run();
    16.  
    17. QDialog* mDialog;
    18. };
    19.  
    20. #endif // DIALOGDESTROYER_H
    To copy to clipboard, switch view to plain text mode 

    dialogdestroyer.cpp:
    Qt Code:
    1. #include <QTimer>
    2.  
    3. #include "dialogdestroyer.h"
    4.  
    5.  
    6. DialogDestroyer::DialogDestroyer() {
    7.  
    8. }
    9.  
    10. void DialogDestroyer::DelayedDestruction(QDialog* lDialog) {
    11.  
    12. mDialog = lDialog;
    13.  
    14. this->start();
    15.  
    16. }
    17.  
    18. void DialogDestroyer::run() {
    19.  
    20. msleep(300);
    21.  
    22. mDialog->deleteLater();
    23. this->deleteLater();
    24. }
    To copy to clipboard, switch view to plain text mode 

    Somewhere:
    Qt Code:
    1. void MainWindow::on_actionOpen_triggered() {
    2.  
    3. QFileDialog* lDiag = new QFileDialog(this, QString("Select file to open"),
    4. QString("D:\\"), QString("Text files (*.txt)"));
    5.  
    6. int lResult = lDiag->exec();
    7.  
    8. if(lResult != QDialog::Rejected) {
    9. // Do stuff...
    10. }
    11.  
    12. (new DialogDestroyer())->DelayedDestruction(lDiag);
    13. }
    To copy to clipboard, switch view to plain text mode 

    There should be a better way/implementation, in my honest opinion.

Similar Threads

  1. Replies: 1
    Last Post: 19th February 2010, 11:02
  2. delete runtime-created widgets causes crash
    By jonasbalmer in forum Qt Programming
    Replies: 5
    Last Post: 10th February 2010, 21:36
  3. closing a Qdialog called from a Qdialog
    By OverTheOCean in forum Qt Programming
    Replies: 3
    Last Post: 28th September 2009, 08:02
  4. Replies: 4
    Last Post: 19th February 2009, 11:10

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.