PDA

View Full Version : MDI application memory issue



sirQit
4th July 2009, 05:43
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:


void MainWindow::on_addMdiChildAction_triggered()
{
MdiDialog* dialog = new MdiDialog(this);
this->ui->mdiArea->addSubWindow(dialog);
dialog->show();
}

Am I missing anything?

Thanks in advance.

genessr
4th July 2009, 06:29
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


#ifndef GENES_H
#define GENES_H
#include <QtGui/QDialog>


namespace Ui {
class genes;
}

class genes : public QDialog {
Q_OBJECT
Q_DISABLE_COPY(genes)
public:
explicit genes(QWidget *parent = 0);
virtual ~genes(); // automatically called when a dialog box is closed

protected:
virtual void changeEvent(QEvent *e);

private:
Ui::genes *m_ui;

};

#endif // GENES_H


Class implementation


#include "genes.h"
#include "ui_genes.h"

// Construtor
genes::genes(QWidget *parent) : QDialog(parent), m_ui(new Ui::genes)
{
m_ui->setupUi(this);
}

// Destructor
genes::~genes()
{
delete m_ui; // deallocate memory used by the class

ChrisW67
5th July 2009, 09:18
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.

wysota
5th July 2009, 11:16
virtual ~genes(); // automatically called when a dialog box is closed


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.

sirQit
8th July 2009, 02:51
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



void MainWindow::on_addMdiAction_triggered()
{
MdiDialog* dialog = new MdiDialog(this);
this->ui->mdiArea->addSubWindow(dialog);
dialog->setAttribute(Qt::WA_DeleteOnClose);
dialog->show();
}

void MainWindow::on_closeCurrentMdiAction_triggered()
{
MdiDialog* dialog = (MdiDialog*)(this->ui->mdiArea->activeSubWindow());
this->ui->mdiArea->removeSubWindow(dialog);
dialog->close();
delete dialog;
}


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.

wysota
8th July 2009, 08:45
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.

sirQit
8th July 2009, 19:11
That sounds like a good reason...

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

nikhilqt
8th July 2009, 19:32
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.