diff options
author | J-P Nurmi <jpnurmi@gmail.com> | 2009-10-20 15:03:10 (GMT) |
---|---|---|
committer | Andreas Aardal Hanssen <andreas.aardal.hanssen@nokia.com> | 2009-11-03 08:34:52 (GMT) |
commit | e7a10b00be3e4aa197900ecf424e6d44b07248ae (patch) | |
tree | 0dfd8694cb81a7a9e22fcbb339cb2e7fe08a0ef5 | |
parent | 11dea4a8b227801c110f791f350632bf6f0c958d (diff) | |
download | Qt-e7a10b00be3e4aa197900ecf424e6d44b07248ae.zip Qt-e7a10b00be3e4aa197900ecf424e6d44b07248ae.tar.gz Qt-e7a10b00be3e4aa197900ecf424e6d44b07248ae.tar.bz2 |
Fixed QGraphicsScene::clear() not to crash if related top level items
happen to delete each others.
Merge-Request: 1863
Reviewed-by: Andreas
Reviewed-by: Olivier
-rw-r--r-- | src/gui/graphicsview/qgraphicsscene.cpp | 5 | ||||
-rw-r--r-- | tests/auto/qgraphicsscene/tst_qgraphicsscene.cpp | 20 |
2 files changed, 23 insertions, 2 deletions
diff --git a/src/gui/graphicsview/qgraphicsscene.cpp b/src/gui/graphicsview/qgraphicsscene.cpp index a62e486..a4965e4 100644 --- a/src/gui/graphicsview/qgraphicsscene.cpp +++ b/src/gui/graphicsview/qgraphicsscene.cpp @@ -2292,8 +2292,9 @@ void QGraphicsScene::clear() // NB! We have to clear the index before deleting items; otherwise the // index might try to access dangling item pointers. d->index->clear(); - const QList<QGraphicsItem *> items = d->topLevelItems; - qDeleteAll(items); + // NB! QGraphicsScenePrivate::unregisterTopLevelItem() removes items + while (!d->topLevelItems.isEmpty()) + delete d->topLevelItems.first(); Q_ASSERT(d->topLevelItems.isEmpty()); d->lastItemCount = 0; d->allItemsIgnoreHoverEvents = true; diff --git a/tests/auto/qgraphicsscene/tst_qgraphicsscene.cpp b/tests/auto/qgraphicsscene/tst_qgraphicsscene.cpp index 0589994..4f76ddd 100644 --- a/tests/auto/qgraphicsscene/tst_qgraphicsscene.cpp +++ b/tests/auto/qgraphicsscene/tst_qgraphicsscene.cpp @@ -1453,6 +1453,13 @@ void tst_QGraphicsScene::focusItemLostFocus() item->clearFocus(); } +class ClearTestItem : public QGraphicsRectItem +{ +public: + ~ClearTestItem() { qDeleteAll(items); } + QList<QGraphicsItem *> items; +}; + void tst_QGraphicsScene::clear() { QGraphicsScene scene; @@ -1463,6 +1470,19 @@ void tst_QGraphicsScene::clear() scene.clear(); QVERIFY(scene.items().isEmpty()); QCOMPARE(scene.sceneRect(), QRectF(0, 0, 100, 100)); + + ClearTestItem *firstItem = new ClearTestItem; + QGraphicsItem *secondItem = new QGraphicsRectItem; + firstItem->items += secondItem; + + scene.setItemIndexMethod(QGraphicsScene::NoIndex); + scene.addItem(firstItem); + scene.addItem(secondItem); + QCOMPARE(scene.items().at(0), firstItem); + QCOMPARE(scene.items().at(1), secondItem); + // must not crash even if firstItem deletes secondItem + scene.clear(); + QVERIFY(scene.items().isEmpty()); } void tst_QGraphicsScene::setFocusItem() |