开发者

QGraphicsScene::clear() method crashes only in release mode

开发者 https://www.devze.com 2023-03-08 15:23 出处:网络
I inherited my scene from QGraphicsScene. I add many items(QGraphicslineItem, QGraphicsItem, QGraphicsTextItem) on this scene. Whenever i t开发者_StackOverflowry QGraphicsSceneClear method it crashes

I inherited my scene from QGraphicsScene. I add many items(QGraphicslineItem, QGraphicsItem, QGraphicsTextItem) on this scene. Whenever i t开发者_StackOverflowry QGraphicsSceneClear method it crashes on release mode. It works fine on debug mode.

Note: Some items have child items and/or have pointers to other items, so when i delete them i handle them in the destructor (deleting pointers to other items etc). i guess pointers to other items makes it crashing but i do not understand why it crashes only on release mode.

The call stack in release mode is not useful, but it does say access violation on the console.


Your problem is some of your items are deleting other items in the same scene. You have no control (well, not directly) on the order of removal of items when calling clear(). Say you have items A, B, and C. C maintains a pointer to both A and B. When clear() is called, A and B may have already been removed and deleted when C's destructor is called.

As for the crash only happening in release mode, the order of removal may depend on the level of compiler optimization. This is really common when dealing with dangling pointer.

0

精彩评论

暂无评论...
验证码 换一张
取 消