<?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[Removing layoutitem and deleting it yet still memory leak]]></title><description><![CDATA[<p dir="auto">Hi,</p>
<p dir="auto">for fun i created some sort of dice rolling app.<br />
It allows you to roll any amount of dices with any amount of pips. The result is shown as simply QLabels with the dices pips on them.<br />
I do this by having a seperate layout for the "result labels" that is added to my central layout and adding the needed amount of QLabels to it with every roll.<br />
At the start of the drawing of the result i simply erase the content of the "result layout".<br />
This apparently creates a memory leak.<br />
Here is the important part of my code:</p>
<p dir="auto">@void Widget::onRoll(void) //this is a slot<br />
{<br />
quint32 dicenumber = diceinput.text().toUInt();//how many dices were used<br />
quint32 dicetype   = this-&gt;dicetype.text().toUInt(); //amount of pips<br />
clearLayout(diceresult);</p>
<pre><code>for(quint32 i = 0; i &lt; dicenumber;i++)
{
    diceresult.addWidget(QPointer&lt;QLabel&gt;(new QLabel(QString::number((qrand() % dicetype) + 1))));
}

update();
</code></pre>
<p dir="auto">}<br />
//this apparently results in a leak<br />
void Widget::clearLayout(QLayout&amp; layout)<br />
{<br />
QLayoutItem* item;<br />
while(item = layout.takeAt(0))<br />
{<br />
item-&gt;widget()-&gt;setVisible(false);<br />
layout.removeWidget(item-&gt;widget());<br />
delete item;<br />
}<br />
}<br />
@</p>
<p dir="auto">When i click the "roll dice" button of my program it calls the "onRoll" slot.<br />
Why does the "delete item" call not lead to all dynamic memory being properly deleted?</p>
<p dir="auto">Btw i figured this had a leak by looking at the rising amount of memory the program used (provided by windows task manager)</p>
<p dir="auto">Any help would be great :)</p>
]]></description><link>https://forum.qt.io/topic/33445/removing-layoutitem-and-deleting-it-yet-still-memory-leak</link><generator>RSS for Node</generator><lastBuildDate>Sun, 27 Sep 2026 06:25:25 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/33445.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 24 Oct 2013 00:35:31 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Removing layoutitem and deleting it yet still memory leak on Thu, 24 Oct 2013 13:14:57 GMT]]></title><description><![CDATA[<p dir="auto">because the item doesn't take ownership of the widget itself... IIRC this should be mentioned somewhere in the docs.</p>
]]></description><link>https://forum.qt.io/post/200322</link><guid isPermaLink="true">https://forum.qt.io/post/200322</guid><dc:creator><![CDATA[raven-worx]]></dc:creator><pubDate>Thu, 24 Oct 2013 13:14:57 GMT</pubDate></item><item><title><![CDATA[Reply to Removing layoutitem and deleting it yet still memory leak on Thu, 24 Oct 2013 12:28:58 GMT]]></title><description><![CDATA[<p dir="auto">Hi,</p>
<p dir="auto">thank you :) This solved it.<br />
But i would like to understand why this worked and my first approach didnt.</p>
<p dir="auto">what i do now is simply<br />
@ QLayoutItem* item;<br />
while((item = layout.takeAt(0)) != 0)<br />
{<br />
item-&gt;widget()-&gt;setVisible(false);<br />
delete item-&gt;widget();<br />
delete item;<br />
}@</p>
<p dir="auto">why is the dynamic memory not released by calling "delete item"?</p>
]]></description><link>https://forum.qt.io/post/200316</link><guid isPermaLink="true">https://forum.qt.io/post/200316</guid><dc:creator><![CDATA[[[global:former-user]]]]></dc:creator><pubDate>Thu, 24 Oct 2013 12:28:58 GMT</pubDate></item><item><title><![CDATA[Reply to Removing layoutitem and deleting it yet still memory leak on Thu, 24 Oct 2013 07:44:58 GMT]]></title><description><![CDATA[<p dir="auto">Hi,</p>
<p dir="auto">You also need to delete the widget</p>
]]></description><link>https://forum.qt.io/post/200288</link><guid isPermaLink="true">https://forum.qt.io/post/200288</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Thu, 24 Oct 2013 07:44:58 GMT</pubDate></item></channel></rss>