<?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[Is this object destruction failure?]]></title><description><![CDATA[<p dir="auto">Why does the following program crash when trying to exit the program?</p>
<pre><code>#include &lt;QApplication&gt;
#include &lt;QLayout&gt;
#include &lt;QLabel&gt;

int main(int argc, char *argv[]) {
    QApplication app(argc, argv);
    QWidget appWidget;

    QLabel lblA("A");
    QGridLayout layG;
    layG.addWidget(&amp;lblA, 0, 0);

    QLabel lblB("B");
    QVBoxLayout totalLayout;
    totalLayout.addLayout(&amp;layG);
    totalLayout.addWidget(&amp;lblB);

    appWidget.setLayout(&amp;totalLayout);
    appWidget.show();

    return app.exec();
}
</code></pre>
<p dir="auto">but if you dynamically allocate memory for just one of <code>layG</code> or <code>totalLayout</code> layouts, it won't crash.<br />
what is happening here?</p>
]]></description><link>https://forum.qt.io/topic/164031/is-this-object-destruction-failure</link><generator>RSS for Node</generator><lastBuildDate>Sun, 19 Apr 2026 00:25:58 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/164031.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 02 Jan 2026 21:41:00 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Is this object destruction failure? on Sat, 03 Jan 2026 00:07:50 GMT]]></title><description><![CDATA[<p dir="auto"><code>QObject</code>s destroy their children when they are destroyed, and a layout is automatically the parent of any sub-layout added to it.</p>
<p dir="auto">Stack objects are destroyed in opposite order to their creation, so <code>totalLayout</code> gets destroyed first, and it also destroys <code>layG</code> since it is its child object in the QObject hierarchy. The next in order is <code>lblB</code>, but after it <code>layG</code> gets destroyed, but it was already deleted so it is a use after free violation and a crash.</p>
<p dir="auto">Dynamically allocating (and leaking) either layout prevents automatic destruction of it so avoids the scenario.</p>
]]></description><link>https://forum.qt.io/post/835126</link><guid isPermaLink="true">https://forum.qt.io/post/835126</guid><dc:creator><![CDATA[IgKh]]></dc:creator><pubDate>Sat, 03 Jan 2026 00:07:50 GMT</pubDate></item><item><title><![CDATA[Reply to Is this object destruction failure? on Sat, 03 Jan 2026 08:42:02 GMT]]></title><description><![CDATA[<p dir="auto">Don't create widgets and especially layouts on the stack at all to avoid such problems.</p>
]]></description><link>https://forum.qt.io/post/835134</link><guid isPermaLink="true">https://forum.qt.io/post/835134</guid><dc:creator><![CDATA[Christian Ehrlicher]]></dc:creator><pubDate>Sat, 03 Jan 2026 08:42:02 GMT</pubDate></item><item><title><![CDATA[Reply to Is this object destruction failure? on Sat, 03 Jan 2026 00:52:41 GMT]]></title><description><![CDATA[<p dir="auto">I get it. but actually QLayout is not responsible for destroying any QWidget that is added to it. QLayouts only destroy QLayouts that are added to them. QWidgets cannot be a "child" of QLayouts. here, <code>totalLayout</code> is responsible for removing <code>layG</code> object, which is its child. So here what actually causes the crash is as follows when stack unwinds at the end:<br />
First, <code>totalLayout</code>'s destructor is called.<br />
Second, in the destructor, <code>totalLayout</code> evokes "delete" operator on <code>layG</code> (cause <code>totalLayout</code> is responsible for removing <code>layG</code> as its child)<br />
and here is the problem. <code>layG</code> is stack-allocated and calling "delete" operator on a stack-allocated object is not valid.</p>
<p dir="auto">So, if we allocate even one of them on the heap, in both scenarios it obviously would resolve this problem, one by memory leaking and the other by properly deallocating object.</p>
<p dir="auto">This is true for the widgets too. the only thing different, is that setLayout() function in widgets makes the widget responsible for the life-time of the QLayouts.</p>
<p dir="auto">Changing the order of Layou creations, in this case matters. If I were to create <code>totalLayout</code> before <code>layG</code>, the crash wouldn't have happened. cause <code>layG</code> would've been properly destroyed before <code>totalLayout</code> tried to evoke "delete" operator on it. Though, I'm not sure how it's handled in Qt.</p>
]]></description><link>https://forum.qt.io/post/835127</link><guid isPermaLink="true">https://forum.qt.io/post/835127</guid><dc:creator><![CDATA[Nima_MJ]]></dc:creator><pubDate>Sat, 03 Jan 2026 00:52:41 GMT</pubDate></item><item><title><![CDATA[Reply to Is this object destruction failure? on Sat, 03 Jan 2026 00:07:50 GMT]]></title><description><![CDATA[<p dir="auto"><code>QObject</code>s destroy their children when they are destroyed, and a layout is automatically the parent of any sub-layout added to it.</p>
<p dir="auto">Stack objects are destroyed in opposite order to their creation, so <code>totalLayout</code> gets destroyed first, and it also destroys <code>layG</code> since it is its child object in the QObject hierarchy. The next in order is <code>lblB</code>, but after it <code>layG</code> gets destroyed, but it was already deleted so it is a use after free violation and a crash.</p>
<p dir="auto">Dynamically allocating (and leaking) either layout prevents automatic destruction of it so avoids the scenario.</p>
]]></description><link>https://forum.qt.io/post/835126</link><guid isPermaLink="true">https://forum.qt.io/post/835126</guid><dc:creator><![CDATA[IgKh]]></dc:creator><pubDate>Sat, 03 Jan 2026 00:07:50 GMT</pubDate></item></channel></rss>