PDA

View Full Version : Does qgraphicsscene schedule updates in a separate thread?



xenome
17th October 2009, 05:21
Hi,

I've noticed that if I add a large number of objects to a scene and then start deleting them very quickly (commanded by a keystroke from my parent widget) I can actually crash qgraphicsscene. I'm not sure what's happening, but I get the impression that while I am deleting the objects qgraphicsscene starts a redraw/update and while it's updating I delete more objects and a pointer probably goes null and it crashes.

When I debug my app during the crash I see the second to last function call is:



class QGraphicsSceneFindItemBspTreeVisitor : public QGraphicsSceneBspTreeVisitor
{
public:
QList<QGraphicsItem *> *foundItems;

void visit(QList<QGraphicsItem *> *items)
{
for (int i = 0; i < items->size(); ++i) {
QGraphicsItem *item = items->at(i);
if (!item->d_func()->itemDiscovered && item->isVisible()) {
item->d_func()->itemDiscovered = 1;
foundItems->prepend(item);
}
}
}
};


The crash is on the d_func() call.

I've added print statements to make sure I am not leaking objects from the scene and everything looks accounted for. In fact, it appears my delete runs and then a fraction of a second later I get the crash while the scene tries to render.

My code is always calling removeItem() first, then delete;

So is it possible that my delete code is running in one thread while the update runs in another and my delete is running "ahead" of the render which causes a problem?

wysota
17th October 2009, 10:10
So is it possible that my delete code is running in one thread while the update runs in another and my delete is running "ahead" of the render which causes a problem?

No, graphics view doesn't use external threads. Your code for deleting items is simply invalid. You can't delete items which are subject to signals or events which is probably what you are doing.

xenome
17th October 2009, 21:08
Thanks for the response.

After talking to some people in the #qt IRC channel they think this might actually be a bug with QGraphicsScene in the above mentioned routine. Someone suggested I turn off BSP indexing (which I did), and the problem went away.

Thanks

medved6
3rd March 2011, 06:37
I've got exactly same issue!
Thanks!

MarekR22
3rd March 2011, 10:23
It looks like a bug.
IMHO when you are want to delete QGraphicsItem you don't have to call anything before! So try omit call of QGraphicsScene::removeItem() when you are planning to delete object it may fix the problem.

Jonathan
17th July 2011, 05:27
Hi,

I am able to repeat this bug 100% reliably in the current build of Fritzing (unfortunately). It takes quite a few steps to set it up, so making a simple case out of it will be very difficult. The bug occurs under Windows 4.7.0 (with a compiled Qt) and also under Mac 4.7.3 with the standard carbon Qt download.

As with the case in this thread (http://lists.trolltech.com/qt-interest/2006-12/thread00069-0.html), it has to do with using a custom boundingRect() function for a QGraphicsItem subclass. My suspicion at the moment, though I have no proof and may be completely wrong, is that it is arising because the boundingRect in this case is highly dynamic.