Memory management in the calendarwidget example. Is there are memory leak?
-
Hello, this is my first time posting here!
I am new to C++ and Qt and I was looking at the example source code. Specifically, this file:
https://github.com/qt/qtbase/blob/dev/examples/widgets/widgets/calendarwidget/window.cpp
and this member function:
void Window::createPreviewGroupBox() { previewGroupBox = new QGroupBox(tr("Preview")); calendar = new QCalendarWidget; calendar->setMinimumDate(QDate(1900, 1, 1)); calendar->setMaximumDate(QDate(3000, 1, 1)); calendar->setGridVisible(true); connect(calendar, &QCalendarWidget::currentPageChanged, this, &Window::reformatCalendarPage); previewLayout = new QGridLayout; previewLayout->addWidget(calendar, 0, 0, Qt::AlignCenter); previewGroupBox->setLayout(previewLayout); }previewGroupBox, calendar and previewLayout are naked pointers to of type QGroupBox* QGridLayout* and QCalendarWidget*. My question is: When and where is the memory allocated using new released? Those objects have no parent to delete them and delete is not called anywhere is the example code.
Thanks in advance!
-
Hello, this is my first time posting here!
I am new to C++ and Qt and I was looking at the example source code. Specifically, this file:
https://github.com/qt/qtbase/blob/dev/examples/widgets/widgets/calendarwidget/window.cpp
and this member function:
void Window::createPreviewGroupBox() { previewGroupBox = new QGroupBox(tr("Preview")); calendar = new QCalendarWidget; calendar->setMinimumDate(QDate(1900, 1, 1)); calendar->setMaximumDate(QDate(3000, 1, 1)); calendar->setGridVisible(true); connect(calendar, &QCalendarWidget::currentPageChanged, this, &Window::reformatCalendarPage); previewLayout = new QGridLayout; previewLayout->addWidget(calendar, 0, 0, Qt::AlignCenter); previewGroupBox->setLayout(previewLayout); }previewGroupBox, calendar and previewLayout are naked pointers to of type QGroupBox* QGridLayout* and QCalendarWidget*. My question is: When and where is the memory allocated using new released? Those objects have no parent to delete them and delete is not called anywhere is the example code.
Thanks in advance!
@dgrigoro
When youaddWidget()orsetLayout()the objects all go into the widget hierarchy ---previewGroupBoxhere --- and you'll seeparentget set. So those now belong to their parent hierarchy and get disposed with that.The
previewGroupBoxis currently indeed orphaned. To use this method one would either add that into the widget hierarchy here or return it from the function so the caller can do that. In fact looking at the link it's a member variable, so it doesn't get lost, and the caller goeslayout->addWidget(previewGroupBox), so that's how it gets connected.If you haven't already done so, you'll want to read https://doc.qt.io/qt-5/objecttrees.html & https://doc.qt.io/qt-5/layout.html.
-
@dgrigoro
When youaddWidget()orsetLayout()the objects all go into the widget hierarchy ---previewGroupBoxhere --- and you'll seeparentget set. So those now belong to their parent hierarchy and get disposed with that.The
previewGroupBoxis currently indeed orphaned. To use this method one would either add that into the widget hierarchy here or return it from the function so the caller can do that. In fact looking at the link it's a member variable, so it doesn't get lost, and the caller goeslayout->addWidget(previewGroupBox), so that's how it gets connected.If you haven't already done so, you'll want to read https://doc.qt.io/qt-5/objecttrees.html & https://doc.qt.io/qt-5/layout.html.