summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSean Harmer <sean.harmer@kdab.com>2020-10-01 10:22:47 +0100
committerSean Harmer <sean.harmer@kdab.com>2020-10-05 10:58:05 +0100
commitca051b7dc9be2836800fef275b502e46423c48c2 (patch)
tree0d55710b4e9c1579785ea840e682a2d922518315
parent2b34df423c1f2d525e341ad11cd2f281d1caf0c6 (diff)
Fix a crash when node is added and destroyed immediately
When a node is added to the scene it is scheduled for a post-creation initialization. However, if the node is destroyed before this post-creation queue has been processed we will crash. This commit ensures that the node being deleted is removed from the post-creation queue. Usually the queue will be empty and so this should not add a performance penalty. Change-Id: Ibe4289e1e54cdb145f8588f15dc4ad894e427582 Reviewed-by: Mike Krus <mike.krus@kdab.com> (cherry picked from commit 0a533673b5450d49725d23e08ac13b03fad564fb)
-rw-r--r--src/core/nodes/qnode.cpp5
-rw-r--r--tests/auto/core/qaspectengine/tst_qaspectengine.cpp47
2 files changed, 52 insertions, 0 deletions
diff --git a/src/core/nodes/qnode.cpp b/src/core/nodes/qnode.cpp
index ad084242f..7ae29f222 100644
--- a/src/core/nodes/qnode.cpp
+++ b/src/core/nodes/qnode.cpp
@@ -141,6 +141,11 @@ void QNodePrivate::notifyDestructionChangesAndRemoveFromScene()
{
Q_Q(QNode);
+ // Ensure this node is not queued up for post-construction init
+ // to avoid crashing when the event loop spins.
+ if (m_scene && m_scene->postConstructorInit())
+ m_scene->postConstructorInit()->removeNode(q);
+
// We notify the backend that the parent lost us as a child
if (m_changeArbiter != nullptr && !m_parentId.isNull()) {
const auto change = QPropertyNodeRemovedChangePtr::create(m_parentId, q);
diff --git a/tests/auto/core/qaspectengine/tst_qaspectengine.cpp b/tests/auto/core/qaspectengine/tst_qaspectengine.cpp
index 48443a66f..759adda63 100644
--- a/tests/auto/core/qaspectengine/tst_qaspectengine.cpp
+++ b/tests/auto/core/qaspectengine/tst_qaspectengine.cpp
@@ -197,6 +197,53 @@ private Q_SLOTS:
// * destroying the aspect engine
}
+ void shouldNotCrashWhenEntityIsAddedThenImmediatelyDeleted()
+ {
+ // GIVEN
+ // An initialized aspect engine...
+ QAspectEngine engine;
+ // ...and a simple aspect
+ PrintRootAspect *aspect = new PrintRootAspect;
+
+ // WHEN
+ // We register the aspect
+ engine.registerAspect(aspect);
+
+ // THEN
+ const auto registeredAspects = engine.aspects();
+ QCOMPARE(registeredAspects.size(), 1);
+ QCOMPARE(registeredAspects.first(), aspect);
+
+ // WHEN
+ QEntityPtr entity(new QEntity);
+ entity->setObjectName("RootEntity");
+ // we set a scene root entity
+ engine.setRootEntity(entity);
+
+ QEventLoop eventLoop;
+ QTimer::singleShot(1000, &eventLoop, SLOT(quit()));
+ eventLoop.exec();
+
+ // THEN
+ // we don't crash and...
+ const auto rootEntity = engine.rootEntity();
+ QCOMPARE(rootEntity, entity);
+
+ // WHEN
+ // we create a child node and delete within the same spin of
+ // the event loop
+ Qt3DCore::QEntity *childEntity = new Qt3DCore::QEntity(entity.data());
+ delete childEntity;
+ entity = nullptr;
+ QTimer::singleShot(600, &eventLoop, SLOT(quit()));
+
+ // ...and allow events to process...
+ eventLoop.exec();
+
+ // ...and we don't crash when the childEntity is removed from the
+ // post construction init routines
+ }
+
void shouldNotCrashOnShutdownWhenComponentIsCreatedWithParentBeforeItsEntity()
{
// GIVEN