Memory Leakage and SubClassing (Custom QWidget) Issue
-
I have an mainwindow app, with 3 main modules and each modules have 6 sub-modules. Each submodel consists of different kind of Gui widgets (like Qlineedit, Qlabel, QPushbutton,QGroupBox etc...).
I would subclass all each main and sub-modules (inheriths QWidget). Each main and sub module is created dynamically. For ex, when the 1st main module is constructed, it also allocates for the 6 submodules. But, when the app is closed, it doesn't free all 6 submodules. There may be a leakage problem but I didn't figure out. why. I think I have a problem with subclassing of my modules.
My app is too big to post here so I would post 2 benchmark codes. To show the issue, this mainwindow app has 100 my custom QLineEdit object (I know it is weird but it adresses my problem well). But when the app is closed, doesn't free all the 100 custom Qlineedit objects. (Here, my custom QLineedit class represents my modules...)
I have tried 2 kind of subclassing to understand the problem.
- BcInputBox inherits QLineEdit
- BcInputBox inherits QWidget
I've tried both of them separately but the issue is the same. I've spent more than 1 week but I haven't figured it out yet.
Which approach would be better? What is the mistake about my design?
Thanks in advance
1st Benchmark
@
// Benchmark 1 //
//
//******************************************
//BcInputBox.h
#ifndef BCINPUTBOX_H
#define BBCINPUTBOX_H#include <QLineEdit>
namespace BC {
const bool ReadOnly = true;
const bool Edit = false;
const bool Double = true;
const bool Name = false;
}class BcInputBox: public QWidget
{
Q_OBJECT
public:
explicit BcInputBox(QWidget *parent = 0, bool editInfo = BC::Edit, bool inputInfo = BC::Double);
~BcInputBox();
void setEditInfo(bool editInfo);
void setInputInfo(bool inputInfo);
QLineEdit *getInputBox() const;
static int objCounter;
private:
QLineEdit *inputBox;
};
#endif // BCINPUTBOX_H//******************************************
//BcInputBox.cpp#include <QDebug>
#include <QHBoxLayout>
#include <QValidator>
#include "BcInputBox.h"
#include "BcDoubleValidator.h"int BcInputBox :: objCounter = 0;
BcInputBox::BcInputBox(QWidget *parent, bool editInfo, bool inputInfo):QWidget(parent)
{QHBoxLayout *hlay = new QHBoxLayout(this); inputBox = new QLineEdit(this); setEditInfo(editInfo); setInputInfo(inputInfo); hlay -> addWidget(inputBox); inputBox -> setStyleSheet("background-color: yellow;" "border-radius: 8px;"); qDebug() << objCounter++ << "BcInputBox()";
}
BcInputBox :: ~BcInputBox()
{
//qDebug() << objCounter ;
qDebug() << "~BcInputBox";
delete inputBox;
}void BcInputBox :: setEditInfo(bool editInfo)
{
inputBox -> setReadOnly(editInfo);
}void BcInputBox :: setInputInfo(bool inputInfo)
{if (!inputInfo){ QRegExp rExp ("[A-Za-z0-9]{1,16}"); inputBox -> setValidator(new QRegExpValidator(rExp, inputBox)); } else { inputBox -> setValidator( new BcDoubleValidator( 0.0, 1000, 3,inputBox)); }
}
QLineEdit BcInputBox :: getInputBox() const
{
return inputBox;
}
//*****************************************
//MainWindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>
#include <QLineEdit>
#include "subconsmod1.h"
#include "BcInputBox.h"namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECTpublic:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();private:
Ui::MainWindow *ui;
BcInputBox *subSection[100];};
#endif // MAINWINDOW_H
//******************************************
//MainWindow.cpp
#include <QtCore>
#include "mainwindow.h"
#include "ui_mainwindow.h"MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QVBoxLayout *hlay = new QVBoxLayout(ui->centralWidget);
for (int i = 0; i < 100; ++i ){
subSection[i] = new BcInputBox (ui->centralWidget);
hlay -> addWidget(subSection[i]);}
ui->centralWidget ->setLayout(hlay);
}MainWindow::~MainWindow()
{
delete ui;
}
@2nd Benchmark
@
// Benchmark 2 //
//
//BcInput.h
#ifndef BCINPUT_H
#define BCINPUT_H#include <QLineEdit>
class BcInput : public QLineEdit
{
Q_OBJECT
public:
explicit BcInput(QWidget *parent = 0);
~BcInput();
static int counter;
signals:public slots:
};
#endif // BCINPUT_H
//******************************************
//BcInput.cpp
#include <QDebug>
#include "bcinput.h"int BcInput :: counter = 0;
BcInput::BcInput(QWidget *parent) :
QLineEdit(parent)
{
qDebug() << "BcInput()";
qDebug() << counter++;
}BcInput :: ~BcInput()
{
qDebug() << "~BcInput()";
}
//******************************************
//MainWindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>
#include "bcinput.h"namespace Ui {
class MainWindow;
}class MainWindow : public QMainWindow
{
Q_OBJECTpublic:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();private:
Ui::MainWindow *ui;
BcInput *subSection[100];
};#endif // MAINWINDOW_H
//******************************************
//MainWindow.cpp
#include <QtGui>
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "bcinput.h"MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QVBoxLayout *hlay = new QVBoxLayout(ui->centralWidget);for(int i = 0; i<100;i++){ subSection[i] = new BcInput(ui->centralWidget); hlay->addWidget(subSection[i]);} ui->centralWidget->setLayout(hlay);
}
MainWindow::~MainWindow()
{
for(int i = 0; i<100;i++)
delete subSection[i];
delete ui;
}
@