<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Memory management in the calendarwidget example. Is there are memory leak?]]></title><description><![CDATA[<p dir="auto">Hello, this is my first time posting here!</p>
<p dir="auto">I am new to C++ and Qt and I was looking at the example source code. Specifically, this file:</p>
<p dir="auto"><a href="https://github.com/qt/qtbase/blob/dev/examples/widgets/widgets/calendarwidget/window.cpp" target="_blank" rel="noopener noreferrer nofollow ugc">https://github.com/qt/qtbase/blob/dev/examples/widgets/widgets/calendarwidget/window.cpp</a></p>
<p dir="auto">and this member function:</p>
<pre><code>void Window::createPreviewGroupBox()
{
    previewGroupBox = new QGroupBox(tr("Preview"));

    calendar = new QCalendarWidget;
    calendar-&gt;setMinimumDate(QDate(1900, 1, 1));
    calendar-&gt;setMaximumDate(QDate(3000, 1, 1));
    calendar-&gt;setGridVisible(true);

    connect(calendar, &amp;QCalendarWidget::currentPageChanged,
            this, &amp;Window::reformatCalendarPage);

    previewLayout = new QGridLayout;
    previewLayout-&gt;addWidget(calendar, 0, 0, Qt::AlignCenter);
    previewGroupBox-&gt;setLayout(previewLayout);
}
</code></pre>
<p dir="auto">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 <strong>new</strong> released? Those objects have no parent to delete them and <strong>delete</strong> is not called anywhere is the example code.</p>
<p dir="auto">Thanks in advance!</p>
]]></description><link>https://forum.qt.io/topic/133938/memory-management-in-the-calendarwidget-example-is-there-are-memory-leak</link><generator>RSS for Node</generator><lastBuildDate>Thu, 14 May 2026 02:00:29 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/133938.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 29 Jan 2022 10:52:04 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Memory management in the calendarwidget example. Is there are memory leak? on Sat, 29 Jan 2022 11:26:06 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/dgrigoro">@<bdi>dgrigoro</bdi></a><br />
When you <code>addWidget()</code> or <code>setLayout()</code> the objects all go into the widget hierarchy --- <code>previewGroupBox</code> here --- and you'll see <code>parent</code> get set.  So those now belong to their parent hierarchy and get disposed with that.</p>
<p dir="auto">The <code>previewGroupBox</code> is 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 <em>member</em> variable, so it doesn't get lost, and the caller goes <code>layout-&gt;addWidget(previewGroupBox)</code>, so that's how it gets connected.</p>
<p dir="auto">If you haven't already done so, you'll want to read <a href="https://doc.qt.io/qt-5/objecttrees.html" target="_blank" rel="noopener noreferrer nofollow ugc">https://doc.qt.io/qt-5/objecttrees.html</a> &amp; <a href="https://doc.qt.io/qt-5/layout.html" target="_blank" rel="noopener noreferrer nofollow ugc">https://doc.qt.io/qt-5/layout.html</a>.</p>
]]></description><link>https://forum.qt.io/post/700660</link><guid isPermaLink="true">https://forum.qt.io/post/700660</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Sat, 29 Jan 2022 11:26:06 GMT</pubDate></item><item><title><![CDATA[Reply to Memory management in the calendarwidget example. Is there are memory leak? on Sat, 29 Jan 2022 11:50:49 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a> Thank you! It's all clear now :)</p>
]]></description><link>https://forum.qt.io/post/700661</link><guid isPermaLink="true">https://forum.qt.io/post/700661</guid><dc:creator><![CDATA[dgrigoro]]></dc:creator><pubDate>Sat, 29 Jan 2022 11:50:49 GMT</pubDate></item><item><title><![CDATA[Reply to Memory management in the calendarwidget example. Is there are memory leak? on Sat, 29 Jan 2022 11:26:06 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/dgrigoro">@<bdi>dgrigoro</bdi></a><br />
When you <code>addWidget()</code> or <code>setLayout()</code> the objects all go into the widget hierarchy --- <code>previewGroupBox</code> here --- and you'll see <code>parent</code> get set.  So those now belong to their parent hierarchy and get disposed with that.</p>
<p dir="auto">The <code>previewGroupBox</code> is 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 <em>member</em> variable, so it doesn't get lost, and the caller goes <code>layout-&gt;addWidget(previewGroupBox)</code>, so that's how it gets connected.</p>
<p dir="auto">If you haven't already done so, you'll want to read <a href="https://doc.qt.io/qt-5/objecttrees.html" target="_blank" rel="noopener noreferrer nofollow ugc">https://doc.qt.io/qt-5/objecttrees.html</a> &amp; <a href="https://doc.qt.io/qt-5/layout.html" target="_blank" rel="noopener noreferrer nofollow ugc">https://doc.qt.io/qt-5/layout.html</a>.</p>
]]></description><link>https://forum.qt.io/post/700660</link><guid isPermaLink="true">https://forum.qt.io/post/700660</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Sat, 29 Jan 2022 11:26:06 GMT</pubDate></item></channel></rss>