[Interest] Re: QGraphicsScene crash after removeItem() and delete item
Jan Kundrát
jkt at flaska.net
Thu Dec 6 02:29:54 CET 2012
On Thursday, 29 November 2012 12:33:58 CEST, Volker Poplawski wrote:
> while (not m_items.isEmpty())
> {
> QGraphicsItem *item = m_items.takeFirst();
> m_scene->removeItem(item); // qtdoc says it's faster to
> remove item first then delete
> delete item;
> }
When you call removeItem, its children are removed from the scene as well; when you delete it, these children will be deleted immediately, too. Are you sure that none of the items in your m_items is a child of any item present earlier in that list?
Have you tried to run your program under valgrind?
And as a style suggestion, it might be better to rework the loop like this:
Q_FOREACH(QGraphicsItem *item, m_items) {
m_scene->removeItem(item);
delete item;
}
m_items.clear();
QList very likely doesn't have much overhead even when used in this way, but it feels wrong to keep deleting a first item from a container which is not a linked list.
> The interesting things is that without the removeItem() call,
> i.e. just calling delete on the items, my program does not
> crash.
This is interesting.
Cheers,
Jan
More information about the Interest
mailing list