From 776a2e83734d5655d18b4da16877ce49134bf2a4 Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Mon, 8 Jan 2018 17:18:16 -0800 Subject: [PATCH] Fix concurrent access crash in avatar mixer --- .../src/avatars/AvatarMixerClientData.h | 5 +++-- libraries/avatars/src/AvatarData.cpp | 17 ++++++++++++----- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/assignment-client/src/avatars/AvatarMixerClientData.h b/assignment-client/src/avatars/AvatarMixerClientData.h index acd9be0702..7a7210a0e8 100644 --- a/assignment-client/src/avatars/AvatarMixerClientData.h +++ b/assignment-client/src/avatars/AvatarMixerClientData.h @@ -116,8 +116,9 @@ public: void setLastOtherAvatarEncodeTime(const QUuid& otherAvatar, const uint64_t& time); QVector& getLastOtherAvatarSentJoints(QUuid otherAvatar) { - _lastOtherAvatarSentJoints[otherAvatar].resize(_avatar->getJointCount()); - return _lastOtherAvatarSentJoints[otherAvatar]; + auto& lastOtherAvatarSentJoints = _lastOtherAvatarSentJoints[otherAvatar]; + lastOtherAvatarSentJoints.resize(_avatar->getJointCount()); + return lastOtherAvatarSentJoints; } void queuePacket(QSharedPointer message, SharedNodePointer node); diff --git a/libraries/avatars/src/AvatarData.cpp b/libraries/avatars/src/AvatarData.cpp index f2053e29d7..94df5bf7f4 100644 --- a/libraries/avatars/src/AvatarData.cpp +++ b/libraries/avatars/src/AvatarData.cpp @@ -530,9 +530,13 @@ QByteArray AvatarData::toByteArray(AvatarDataDetail dataDetail, quint64 lastSent destinationBuffer += numValidityBytes; // Move pointer past the validity bytes + // sentJointDataOut and lastSentJointData might be the same vector + // build sentJointDataOut locally and then swap it at the end. + QVector localSentJointDataOut; if (sentJointDataOut) { - sentJointDataOut->resize(_jointData.size()); // Make sure the destination is resized before using it + localSentJointDataOut.resize(numJoints); // Make sure the destination is resized before using it } + float minRotationDOT = !distanceAdjust ? AVATAR_MIN_ROTATION_DOT : getDistanceBasedMinRotationDOT(viewerPosition); for (int i = 0; i < _jointData.size(); i++) { @@ -552,8 +556,7 @@ QByteArray AvatarData::toByteArray(AvatarDataDetail dataDetail, quint64 lastSent destinationBuffer += packOrientationQuatToSixBytes(destinationBuffer, data.rotation); if (sentJointDataOut) { - auto jointDataOut = *sentJointDataOut; - jointDataOut[i].rotation = data.rotation; + localSentJointDataOut[i].rotation = data.rotation; } } @@ -602,8 +605,7 @@ QByteArray AvatarData::toByteArray(AvatarDataDetail dataDetail, quint64 lastSent packFloatVec3ToSignedTwoByteFixed(destinationBuffer, data.translation, TRANSLATION_COMPRESSION_RADIX); if (sentJointDataOut) { - auto jointDataOut = *sentJointDataOut; - jointDataOut[i].translation = data.translation; + localSentJointDataOut[i].translation = data.translation; } } @@ -646,6 +648,11 @@ QByteArray AvatarData::toByteArray(AvatarDataDetail dataDetail, quint64 lastSent if (outboundDataRateOut) { outboundDataRateOut->jointDataRate.increment(numBytes); } + + if (sentJointDataOut) { + // Push new sent joint data to sentJointDataOut + sentJointDataOut->swap(localSentJointDataOut); + } } int avatarDataSize = destinationBuffer - startPosition;