Search code examples
c++linuxqtramqwidget

RAM problems while creating and deleting custom QWidgets inside of QLayout


I have created a custom QWidget (code below) [with a QHBoxLayout and two QPushButtons inside] and added it to a QVBoxLayout in GUI. This custom QWidget-object will be created and deleted several times (code below).

When I type top inside into the console (on embedded linux) there is a RAM-increase every time I add a new QWidget. That's Ok! But I can't see a decrease of RAM on deletion.

What is wrong with my code? I want, that the RAM decreases on deletion of the custom QWidgets.

myCustomWidget.h

class QCustomPushButton_withinIcon_LeftAndRight : public QWidget {
    Q_OBJECT

public:
    //functions
    QCustomPushButton_withinIcon_LeftAndRight(QWidget *parent = 0);
    ~QCustomPushButton_withinIcon_LeftAndRight();

    //other slots like:
    // - virtual void mousePressEvent();
    // - virtual void mouseReleaseEvent();
    //other signals like:
    // - void clicked();
    //other functions like:
    // - virtual void setEnabled(bool a);
    // - virtual void paintEvent(QPaintEvent *);
    // - ...

private:
    //variables
    QLayout *innerLayout;           //!< Layout for two buttons
    QPushButton *buttonLeft;        //!< Button left
    QPushButton *buttonRight;       //!< Button right

    //other variables like:
    // - bool enabled;
    // - ...
};

myCustomWidget.cpp

QCustomPushButton_withinIcon_LeftAndRight::QCustomPushButton_withinIcon_LeftAndRight(QWidget *parent) :
    QWidget(parent),
    mSVG (new svg),
    mGradient (new gradient)
{
    enabled = true;

    //create innerLayout
    innerLayout = new QHBoxLayout();
    this->setLayout(innerLayout);

    //create buttons
    buttonLeft = new QPushButton();
    buttonRight = new QPushButton();
    innerLayout->addWidget(buttonLeft);
    innerLayout->addWidget(buttonRight);

    //create connections like:
    // - connect (buttonLeft, SIGNAL(pressed()), this, SLOT(mousePressEvent()));
    // - ...

    //set some stylesheets
    // - buttonLeft->setStyleSheet("...");
}
QCustomPushButton_withinIcon_LeftAndRight::~QCustomPushButton_withinIcon_LeftAndRight()
{
    //I think, that this is right. If not, correct me.
    delete buttonLeft;
    delete buttonRight;
    delete innerLayout;
}

//void QCustomPushButton_withinIcon_LeftAndRight::mousePressEvent() {}
//void QCustomPushButton_withinIcon_LeftAndRight::mouseReleaseEvent() {}
//void QCustomPushButton_withinIcon_LeftAndRight::setEnabled(bool a) {}
//void QCustomPushButton_withinIcon_LeftAndRight::paintEvent(QPaintEvent *) {} ...

gui.cpp (add QWidgets to a QLayout in the GUI)

    QCustomPushButton_withinIcon_LeftAndRight *button = new QCustomPushButton_withinIcon_LeftAndRight();
    //add button to layout
    parentUiWindow->aLayoutNameInGui->addWidget(button);
    //aLayoutNameInGui is type of QVBoxLayout

gui.cpp (delete QWidgets in a QLayout that is in the GUI)

    //delete all buttons in layout
    QLayoutItem *child;
    while((child = parentUiWindow->aLayoutNameInGui->layout()->takeAt(0)) != 0) {
        //parentUiWindow->aLayoutNameInGui->removeWidget(child->widget()); //already removed by ->takeAt()
        //child->widget()->setParent(NULL);
        delete child->widget();
        delete child;
    }

Solution

  • When you look at the memory usage with the top command, you can get false positives. As stated by some programmer dude, the OS doesn't always release the allocated memory from your process when you call delete on some objects.

    However, you are creating two objects in your QCustomPushButton_withinIcon_LeftAndRight constructor with new:

    mSVG (new svg),
    mGradient (new gradient)
    

    but you never seem to destroy these objects. So you might have a memory leak there.