Results 1 to 8 of 8

Thread: MDI application memory issue

  1. #1
    Join Date
    Jul 2009
    Posts
    4
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default MDI application memory issue

    Hi all

    I am trying to make an MDI application, I am able to add MDI child windows to my main window, the problem is that every time I create a new MDI child some memory is allocated and this memory is not freed after I close the MDI child.

    This is what I have done

    Created a new app, added a new QMdiArea object to the QMainWindow through the designer, then whenever I need to create a new MDI child, I call this:

    Qt Code:
    1. void MainWindow::on_addMdiChildAction_triggered()
    2. {
    3. MdiDialog* dialog = new MdiDialog(this);
    4. this->ui->mdiArea->addSubWindow(dialog);
    5. dialog->show();
    6. }
    To copy to clipboard, switch view to plain text mode 

    Am I missing anything?

    Thanks in advance.

  2. #2
    Join Date
    Jun 2009
    Location
    Bato, Leyte, Philippines
    Posts
    10
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: MDI application memory issue

    Destructors
    What is the use of Destructors

    Destructors are also special member functions used in C++ programming language. Destructors have the opposite function of a constructor. The main use of destructors is to release dynamic allocated memory. Destructors are used to free memory, release resources and to perform other clean up. Destructors are automatically named when an object is destroyed. Like constructors, destructors also take the same name as that of the class name.

    General Syntax of Destructors

    ~ classname();

    The above is the general syntax of a destructor. In the above, the symbol tilda ~ represents a destructor which precedes the name of the class.

    Sample Class :

    Class Definition

    Qt Code:
    1. #ifndef GENES_H
    2. #define GENES_H
    3. #include <QtGui/QDialog>
    4.  
    5.  
    6. namespace Ui {
    7. class genes;
    8. }
    9.  
    10. class genes : public QDialog {
    11. Q_OBJECT
    12. Q_DISABLE_COPY(genes)
    13. public:
    14. explicit genes(QWidget *parent = 0);
    15. virtual ~genes(); // automatically called when a dialog box is closed
    16.  
    17. protected:
    18. virtual void changeEvent(QEvent *e);
    19.  
    20. private:
    21. Ui::genes *m_ui;
    22.  
    23. };
    24.  
    25. #endif // GENES_H
    To copy to clipboard, switch view to plain text mode 


    Class implementation

    Qt Code:
    1. #include "genes.h"
    2. #include "ui_genes.h"
    3.  
    4. // Construtor
    5. genes::genes(QWidget *parent) : QDialog(parent), m_ui(new Ui::genes)
    6. {
    7. m_ui->setupUi(this);
    8. }
    9.  
    10. // Destructor
    11. genes::~genes()
    12. {
    13. delete m_ui; // deallocate memory used by the class
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 5th July 2009 at 11:13. Reason: reformatted to look better

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

    sirQit (8th July 2009)

  4. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: MDI application memory issue

    You are creating the MdiDialog instance as a child of MainWindow. Qt will clean this up for you (i.e. execute delete) when the MainWindow is destroying itself. If you want the memory freed earlier you can do it yourself at a suitable time. Setting Qt::WA_DeleteOnClose on the MdiDialog may also be worth reading about.

  5. The following user says thank you to ChrisW67 for this useful post:

    sirQit (8th July 2009)

  6. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: MDI application memory issue

    Quote Originally Posted by genessr View Post
    Qt Code:
    1. virtual ~genes(); // automatically called when a dialog box is closed
    To copy to clipboard, switch view to plain text mode 
    That's not true. The object is not destroyed when you close the widget. You wouldn't be able to retrieve any data from it. You have to delete it yourself or wait for Qt to do it when the parent is destroyed. You can also set the DeletOnClose flag, as already mentioned by Chris.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. The following user says thank you to wysota for this useful post:

    sirQit (8th July 2009)

  8. #5
    Join Date
    Jul 2009
    Posts
    4
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: MDI application memory issue

    Thank you all for your comments:

    You're actually right, the widget destructor is called automatically when the window is closed, but somehow the memory is not freed up properly, I am looking at the widnows task manager to check this, it allocates some amount of memory when the new MDI window is created and shown, but this memory is not freed up even after the widget destructor is called, this is very weird, shouldn't a C/C++ destructor free the memory by itself?.

    I tried the DeleteOnClose, this made no difference, the memory is still in use after the child MDI is closed.

    Is there any method I should be calling?

    I even tried this in the MDI parent window

    Qt Code:
    1. void MainWindow::on_addMdiAction_triggered()
    2. {
    3. MdiDialog* dialog = new MdiDialog(this);
    4. this->ui->mdiArea->addSubWindow(dialog);
    5. dialog->setAttribute(Qt::WA_DeleteOnClose);
    6. dialog->show();
    7. }
    8.  
    9. void MainWindow::on_closeCurrentMdiAction_triggered()
    10. {
    11. MdiDialog* dialog = (MdiDialog*)(this->ui->mdiArea->activeSubWindow());
    12. this->ui->mdiArea->removeSubWindow(dialog);
    13. dialog->close();
    14. delete dialog;
    15. }
    To copy to clipboard, switch view to plain text mode 

    and closed the active window direct from the main window, no difference, the memory is still not freed.

    I believe I must be missing something, because this looks like pretty straight forward functionality.

    Thanks in advance again.

  9. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: MDI application memory issue

    The task manager is probably wrong, don't worry. Maybe your system is wise enough to keep the memory allocated for the process in case it wants it back soon, just like other intelligent systems do.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. The following user says thank you to wysota for this useful post:

    sirQit (8th July 2009)

  11. #7
    Join Date
    Jul 2009
    Posts
    4
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: MDI application memory issue

    That sounds like a good reason...

    If I have memory issues in the future, I'll get back to you!!!

  12. #8
    Join Date
    Jan 2008
    Location
    Bengaluru
    Posts
    144
    Thanks
    8
    Thanked 7 Times in 7 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: MDI application memory issue

    you try minimizing the mainwindow once and check out your memory in task manager. There will be drastic decrease in the memory. In windows, memory is deallocated when the window is minimized, don know whether this is a normal process in windows.

Similar Threads

  1. How Qt update framebuffer memory
    By nrabara in forum Newbie
    Replies: 0
    Last Post: 25th April 2009, 13:20
  2. Memory debugging in windows
    By txandi in forum Qt Programming
    Replies: 3
    Last Post: 20th February 2009, 13:45
  3. memory leak question
    By cool_qt in forum General Programming
    Replies: 3
    Last Post: 20th January 2009, 07:49
  4. Memory leak weirdness
    By Darhuuk in forum General Programming
    Replies: 10
    Last Post: 10th January 2008, 18:51
  5. Memory Leak in my Application :-(
    By Svaths in forum Qt Programming
    Replies: 4
    Last Post: 27th July 2007, 19:42

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.