I am encountering a strange issue trying to perform multi-threaded updates to the tranformation matrices of QEntities in a Qt3D scene.
The scene consists of about 2000 entities that need to be animated by updating their transformation matrices. The illustrative code below works perfectly:
QHash<Qt3DCore::QTransform *, QMatrix4x4> transforms;
// ... populate with the transformation matrices of ~2000 CylinderMesh entities
for(auto&& transform: transforms.keys())
transform->setMatrix(transforms[transform]);
In order to speed this up, I thought I would make it multi-threaded as follows:
QtConcurrent::blockedMap(transforms.keys(),
[transforms](Qt3DCore::QTransform *t){
t->setMatrix(transforms[t]);
});
This code, however, crashes somewhere inside the Qt objects (stack trace below).
I don't discount that there's an error elsewhere in my code but given that it works fine in a single-threaded way, I'm starting to question whether it is safe to make multithreaded changes to a Qt3D scene?
Here's the stack at crash time:
1 ntdll!RtlRegisterSecureMemoryCacheCallback
2 ntdll!RtlZeroHeap
3 ntdll!EtwLogTraceEvent
4 ntdll!RtlRegisterSecureMemoryCacheCallback
5 ntdll!EtwLogTraceEvent
6 ntdll!RtlReAllocateHeap
7 ntdll!RtlReAllocateHeap
8 realloc
9 QArrayData::reallocateUnaligned qarraydata.cpp 224
10 QTypedArrayData<Qt3DCore::QNode *>::reallocateUnaligned qarraydata.h 117
11 QtPrivate::QPodArrayOps<Qt3DCore::QNode *>::reallocate qarraydataops.h 259
12 QArrayDataPointer<Qt3DCore::QNode *>::reallocateAndGrow qarraydatapointer.h 208
13 QArrayDataPointer<Qt3DCore::QNode *>::detachAndGrow qarraydatapointer.h 194
14 QtPrivate::QPodArrayOps<Qt3DCore::QNode *>::emplace<Qt3DCore::QNode *&> qarraydataops.h 174
15 QList<Qt3DCore::QNode *>::emplaceBack<Qt3DCore::QNode *&> qlist.h 856
16 QList<Qt3DCore::QNode *>::append qlist.h 433
17 QList<Qt3DCore::QNode *>::operator+= qlist.h 681
18 Qt3DCore::QChangeArbiter::addDirtyFrontEndNode qchangearbiter.cpp 47
19 Qt3DCore::QNodePrivate::propertyChanged qnode.cpp 338
20 Qt3DCore::QNodePrivate::propertyChanged qnode.cpp 330
... <more>