PDA

View Full Version : Can't delete widget returned by QLayoutItem->widget()



davethomaspilot
30th May 2014, 00:41
An exception is raised on the line with delete widget. glibc complains:

free(): invalid pointer: 0xbe80291c



void MyServer::clearMyLayout(void)
{
QLayout *currentLayout = window->layout();

if (currentLayout != NULL)
{
QLayoutItem* item;
while ((item= currentLayout->takeAt(0)) != 0)
{
QWidget *widget;
if ((widget = item->widget()) != 0)
delete widget; // SigAbort happens here
delete item;
}
}
delete window->layout();
}


I'm trying to remove all the widgets in a QHBoxLayout. I read that you must delete the widgets and if only delete the item, the widgets are still visible in the parent widget. I found that code fragment along with discussions about the parent being the widget, no the layout.

But shouldn't I be able to delete any non-zero pointer returned by QLayoutItem->widget()?

Thanks,

Dave Thomas

Added after 1 32 minutes:

One of the widgets in the layout was a private data member of an object--not a pointer.

That eliminates the exception--hoping it now works.

davethomaspilot
30th May 2014, 01:18
So, deleting the widgets in a layout works. The exception was caused by trying to delete a widget which was a data member of an object.

For confirmation, is it really necessary to delete the widgets in addition to removing them from a layout? For performance reasons, I'd rather keep the widgets around (QLabels) and just assign new QPixmaps to them.

Dave Thomas

wysota
30th May 2014, 07:45
For confirmation, is it really necessary to delete the widgets in addition to removing them from a layout?
It depends what you want to do with them afterwards. Basically you have to delete any widget without a parent. Removing a widget from a layout doesn't remove it from the parent so explicit deletion is not required as the widget will be deleted together with its parent.


For performance reasons, I'd rather keep the widgets around (QLabels) and just assign new QPixmaps to them.

Then do exactly that.

davethomaspilot
1st June 2014, 11:03
Hmm, so I want to keep the widgets (labels) but display only a subset of them. And, their pixmaps will change.

I thought I read the only way to get them not to appear was to actually delete them.


Then do exactly that.

Removing a widget from a layout doesn't prevent it from displaying. It's just not in the layout.

So, how do I prevent a widget from displaying without deleting it?

d_stranz
1st June 2014, 16:52
So, how do I prevent a widget from displaying without deleting it?

QWidget::hide()

davethomaspilot
1st June 2014, 19:36
If I have three widgets in s QHBoxLayout and hide 1, will the other two align as if the widget was deleted?

If so, that's what I need!

Dave Thomas

davethomaspilot
1st June 2014, 22:39
Yes, that works fine.

So simple--I wasted SO much time chasing a catastrophic memory leak.

I couldn't isolate the problem, but it was somewhere where I was deleting widgets from the HBoxLayout, creating them again, assigning pixmaps, etc.

Valgrind didn't complain, but available memory would continue to decrease until the app was killed (after a couple of hours).

Anyway, I got rid of all that dynamic memory allocation. The widgets are always in the layout (and a containing widget). But they get new pixmaps and subsets are dynamically setVisible(true/false);

Just what I wanted.

Thanks!

Dave Thomas