I'm doing a project using Qt with some customized QDialogs for user input. Due to hardware constraint of my development box, I want to monitor the memory usage of my app. How I exec the dialog.
1 void MainWindow::callDialog() {
2 DlgPopConfig dialog(&theApp->cfgPop, m_fnPopCfg, this);
3 dialog.exec();
4 m_fnPopCfg = dialog.fileName();
5 lbl_fnPopCfg->setText(m_fnPopCfg);
6 }
As the dialog is a local variable, I expect it to be created on stack and destroy immediately once the function ends (after line 5). When the app repeatedly open and close the dialog, its mem usage goes up, and it never return to initial values ['Memory (Working Set)' and 'Memory (Private Working Set)'
columns of Task Manager]. I used Application Verifier, enabling all the basic tests, and it shows no error'. The memory pattern looks like follow (numeric values are made-up of illustration only):
- Application start (working set = 12000K, private set = 6000K)
- Open Dialog-1 (working set = 14000K, private set = 7000K)
- Close Dialog (working set = 12010K, private set = 6005K)
- Open Dialog-2 (working set = 14020K, private set = 7000K)
- Close Dialog (working set = 12010K, private set = 6008K)
- Open Dialog-3 (working set = 14080K, private set = 7010K)
- Close Dialog (working set = 12040K, private set = 6008K)
- ...
So, any idea to trace the root cause of the problem? (Actually, I'm also facing similar issue when usage static methods of QFileDialog getOpenFileName
, getSaveFileName
, and found some discussion here, but it seems not solved)
Edit I use QFormLayout in my dialogs, and I add widgets by layout->addRow("label text", mywidget);
, I doubt if the object destruction fail to remove the labels cleanly.
Edit I created a test program with the QDialog have ten QLineEdits, using same add-widget strategy. The problem still exists. (The problem will happen for this test program if I create and close the dialog frequently, says 10 times in a second)
mainwindow.h
#include <QMainWindow>
#include <QPushButton>
#include <QDialog>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
private:
QPushButton * button;
private slots:
void button_click();
};
class Dialog : public QDialog
{
Q_OBJECT
public:
explicit Dialog(QWidget *parent = 0);
};
mainwindow.cpp
#include "mainwindow.h"
#include <QApplication>
#include <QFormLayout>
#include <QLineEdit>
#include <QLabel>
MainWindow::MainWindow(QWidget *parent):QMainWindow(parent)
{
button=new QPushButton(this);setCentralWidget(button);
connect(button,SIGNAL(clicked()),SLOT(button_click()));
}
void MainWindow::button_click()
{
Dialog d(this);
d.exec();
}
Dialog::Dialog(QWidget *parent):QDialog(parent)
{
QFormLayout*layout=new QFormLayout(this);
setLayout(layout);
for (int i = 0; i < 10; i++)
{
layout->addRow(QString("%1").arg(i+1), new QLineEdit(this));
}
}
int main(int c,char *argv[])
{
QApplication a(c,argv);
MainWindow w;
w.show();
return a.exec();
}
Platform
- Win 7 x64, MinGW 4.7.2 x64 (rubenvb-build), 4GB ram
- Qt 4.8.5 (built natively using above tool-chain)
- Qt-Creator 2.6.1 (built natively using above tool-chain)