Search code examples
c++qtqwidget

why do i have memory leak while doing inheritance of QWidget class in Qt


I am new to c++. I am trying to design one class by inheriting base class QWidget but I am getting memory leak while creating this class via new. here below I have my code snippet of class and main application

#include "QWidget"
#include "QDebug"
#include "ui_myobject.h"

namespace Ui {    
    class MyObject;    
}

class MyObject : public QWidget{    
    Q_OBJECT    
public:       
    explicit MyObject();
    ~MyObject();
    Ui::MyObject *ui; 
};

#include "myobject.h"
#include "ui_myobject.h"

MyObject::MyObject() : QWidget(),ui(new Ui::MyObject){    
    ui->setupUi(this);
    qDebug() << "MyObject Initilised";
}
MyObject::~MyObject(){    
    delete ui;
    qDebug() << "MyObject Deinitilised";    
}

class Application : public QWidget
{
    Q_OBJECT    
public:
    explicit Application(QWidget *parent = 0);    
    ~Application();     
    MyObject *m_MyObject;       
};  

Application::Application(QWidget *parent) :QWidget(parent),ui(new Ui::Application){    
ui->setupUi(this);    
}    

i am calling this below function via click release slot of button

void somefunction()
    {
      m_MyObject = new MyObject();
                //mAlarm_main->Alarm();    
                m_MyObject->show();    
                delete m_MyObject;
    }


int main(int argc, char *argv[])
{
#if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
    ; // Qt5 uses different graphical backend
#else
    QApplication::setGraphicsSystem("raster");
#endif

    QApplication a(argc, argv);
    Application w;

    // uncomment this line to remove window frame
    //w.setWindowFlags(Qt::FramelessWindowHint);

    w.setGeometry(0,0,800,480);

    w.show();

    return a.exec();
}

When I call somefunction I have memory leak in my app I am watching its stack size using top command it continuously increase by 2 mb after 200 timesmy application crashes. I am deleting my object but still some memory leaks occure is there any different way to delete QWidget

If i don't call m_MyObject->show(); function than memory leaks not happen.


Solution

  • In the following piece of code you delete just shown widget (method `show' is not blocking). I believe it causes undefined behavior and, probably, the memory leak you are worrying about:

      m_MyObject = new MyObject();
      m_MyObject->show();
      delete m_MyObject;
    

    Assuming that you need only one MyObject in a time, I would suggest to create MyObject only once (don't forget to initialize it with nullptr by default)

    if (!m_MyObject)
        m_MyObject = new MyObject();
    m_MyObject->show();
    

    Another way (looks like this widget is supposed to be shown as a separate window) is to set Qt::WA_DeleteOnClose attribute (see QWidget::close for details).

        m_MyObject = new MyObject();
        m_MyObject->setAttribute(Qt::WA_DeleteOnClose, true);
        m_MyObject->show();
    

    Example:

    #include <QApplication>
    #include <QPushButton>
    
    int main(int argc, char* argv[])
    {
        QApplication app(argc, argv);
    
        QWidget* w = nullptr;
        QPushButton b;
        b.setText("Button");
        b.connect(&b, &QPushButton::clicked, [&w]() {
            w = new QWidget();
            w->setAttribute(Qt::WA_DeleteOnClose, true); // ADD THIS LINE
            w->show();
            // delete w; // DON'T DO IT
        });
        b.show();
    
        return app.exec();
    }