Does this code provide memory leaks?

Finally, I installed Ubuntu and configured Qt + Valgrind to prevent memory leaks that I could not do on Windows. So I can’t figure out if this code provides a memory leak? In fact, Valgrind says that I have only 500 questions, but nothing about the leak. I

#include <QWidget>
#include <QFrame>
#include <QVBoxLayout>
#include <QApplication>

int main(int argc, char *argv[])

{
    QApplication a(argc, argv);

    QWidget * wdgt = new QWidget;  //this line should be the cause of leakage 
                                   //if it exist (as far as i know)
    QVBoxLayout *layout = new QVBoxLayout;
    QFrame * frame = new QFrame;

    frame->setFrameStyle(QFrame::Panel | QFrame::Plain);
    frame->setLineWidth(5);
    layout->addWidget(frame);

    wdgt->setLayout(layout);
    wdgt->setFixedSize(800,600);
    wdgt->show();

    return a.exec();
}
+4
source share
3 answers

See this post: Creating and releasing a Qt widget object

It is explained that if one Qt object has a parent element, it will be automatically deleted when the parent is destroyed.

In your code:

  • wdgtis a parent layoutbecause you did wdgt->setLayout(layout).
  • wdgt frame, layout->addWidget(frame) layout parent wdgt. thuga, .

wdgt ( Qt, ).

, :

QWidget * wdgt = new QWidget(&app);

, wdgt app app.

:

int main(int argc, char *argv[])
{
    ...
    int res = a.exec();
    delete wdgt; // this will delete wdgt, but also frame and layout
    return res;
}

, fianlly, , :

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QWidget wdgt;

    QVBoxLayout *layout = new QVBoxLayout;
    QFrame * frame = new QFrame;

    frame->setFrameStyle(QFrame::Panel | QFrame::Plain);
    frame->setLineWidth(5);
    layout->addWidget(frame);

    wdgt.setLayout(layout);
    wdgt.setFixedSize(800,600);
    wdgt.show();

    return a.exec();
}

, , QVBoxLayout *layout = new QVBoxLayout(wdgt), wdgt->setLayout(layout). , :

QVBoxLayout *layout = new QVBoxLayout(wdgt); // parenting upon construction

:

QVBoxLayout *layout = new QVBoxLayout; // no parent
wdgt->setLayout( layout ); // reparenting
+8

, , new Qt.

QApplication a(argc, argv);
QWidget * wdgt = new QWidget(&app);
QVBoxLayout *layout = new QVBoxLayout(wdgt); // optional, setLayout does that
QFrame * frame = new QFrame(layout); // optional, addWidget does that

Qt.


++ 11:

QApplication a(argc, argv);
std::shared_ptr<QWidget> wdgt = std::make_shared<QWidget>();

QVBoxLayout *layout = new QVBoxLayout;
QFrame * frame = new QFrame;

, .

0

Your memory leak code, but first of all you should not write code where you even need to take care of the resource leak. Let the compiler handle this for you:

// main.cpp
#include <QtWidgets>

int main(int argc, char *argv[]) {
    QApplication a(argc, argv);
    QWidget widget;
    QVBoxLayout layout(&widget);
    QFrame frame;

    frame.setFrameStyle(QFrame::Panel | QFrame::Plain);
    frame.setLineWidth(5);
    layout.addWidget(&frame);

    widget.setFixedSize(800,600);
    widget.show();
    return a.exec();
}
0
source

Source: https://habr.com/ru/post/1614341/


All Articles