<?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[Confused about memory management]]></title><description><![CDATA[<p dir="auto">I have done some research and it seems like the safe way is to declare a parent for all the widgets. I know that some of this is done implicitly (for example when you add a layout or widget). However, for my code I did it for everything just to be thorough. Yet, when I run valgrind I still get memory leaks. Anyone know what can cause these memory leaks?</p>
<pre><code>testGUI::testGUI(char** argv, QWidget* parent) :
	QWidget(parent), _argv(argv) {

	    // Main window and layout
	QWidget* mainWindow = new QWidget;
	QVBoxLayout *mainLayout = new QVBoxLayout(mainWindow);

	// Tab widget
	QTabWidget* tabWidget = new QTabWidget(mainWindow);

	// The pages in the tab widget
	QWidget* uInt8Window = new QWidget(mainWindow);
	uInt8Window-&gt;setWindowTitle(QString("Page 1"));

	QTableView* tableView = new QTableView(mainWindow);
	QStandardItemModel* model = new QStandardItemModel(5, 5, mainWindow);
	for (int row = 0; row &lt; 5; ++row) {
		model-&gt;setItem(row, 0, new QStandardItem("3"));
		model-&gt;setItem(row, 1, new QStandardItem(5));
		model-&gt;setItem(row, 2, new QStandardItem(2));
		model-&gt;setItem(row, 3, new QStandardItem(1));
		model-&gt;setItem(row, 4, new QStandardItem(5));
	}

	tableView-&gt;setModel(model);

	// Setting the tab page layouts
	_layout = new QVBoxLayout(mainWindow);
	_layout-&gt;addWidget(tableView);

	uInt8Window-&gt;setLayout(_layout);

	// Add the pages to the tab widget
	tabWidget-&gt;addTab(uInt8Window, "Page 1");

	// Add the tab widget to the main layout and show
	mainLayout-&gt;addWidget(tabWidget);
	mainWindow-&gt;setLayout(mainLayout);
	mainWindow-&gt;show();

}
</code></pre>
]]></description><link>https://forum.qt.io/topic/55453/confused-about-memory-management</link><generator>RSS for Node</generator><lastBuildDate>Sun, 06 Sep 2026 11:04:28 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/55453.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 19 Jun 2015 11:56:26 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Confused about memory management on Fri, 19 Jun 2015 20:51:02 GMT]]></title><description><![CDATA[<p dir="auto">Unless I'm mistaken you gave a parent to everything except <code>mainWindow</code>.</p>
<p dir="auto">As your testGUI class inherits QWidget I'm guessing you should have done this: <code>QWidget* mainWindow = new QWidget(this);</code> and remove the <code>mainWindow-&gt;show();</code> call (assuming you show testGUI instance somewhere).</p>
<p dir="auto">Note though that this way <code>mainWindow</code> will not be part of any layout so resizing and such won't work. Since testGUI is already a widget you don't really need mainWindow here. Just set the layout on your testGUI instance: <code>this-&gt;setLayout(mainLayout)</code>.</p>
]]></description><link>https://forum.qt.io/post/278832</link><guid isPermaLink="true">https://forum.qt.io/post/278832</guid><dc:creator><![CDATA[Chris Kawa]]></dc:creator><pubDate>Fri, 19 Jun 2015 20:51:02 GMT</pubDate></item></channel></rss>