The weak shared object hash can be accessed by multiple threads, so it must

be locked.
This commit is contained in:
Andrzej Kapolka 2014-07-15 16:52:17 -07:00
parent 469e31cc05
commit ce0210bf04
2 changed files with 10 additions and 0 deletions

View file

@ -14,6 +14,7 @@
#include <QItemEditorFactory>
#include <QMetaProperty>
#include <QVBoxLayout>
#include <QWriteLocker>
#include "Bitstream.h"
#include "MetavoxelUtil.h"
@ -27,10 +28,12 @@ SharedObject::SharedObject() :
_remoteID(0),
_remoteOriginID(0) {
QWriteLocker locker(&_weakHashLock);
_weakHash.insert(_id, this);
}
void SharedObject::setID(int id) {
QWriteLocker locker(&_weakHashLock);
_weakHash.remove(_id);
_weakHash.insert(_id = id, this);
}
@ -41,6 +44,7 @@ void SharedObject::incrementReferenceCount() {
void SharedObject::decrementReferenceCount() {
if (!_referenceCount.deref()) {
QWriteLocker locker(&_weakHashLock);
_weakHash.remove(_id);
delete this;
}
@ -127,6 +131,7 @@ void SharedObject::dump(QDebug debug) const {
int SharedObject::_lastID = 0;
WeakSharedObjectHash SharedObject::_weakHash;
QReadWriteLock SharedObject::_weakHashLock;
void pruneWeakSharedObjectHash(WeakSharedObjectHash& hash) {
for (WeakSharedObjectHash::iterator it = hash.begin(); it != hash.end(); ) {

View file

@ -17,6 +17,7 @@
#include <QMetaType>
#include <QObject>
#include <QPointer>
#include <QReadWriteLock>
#include <QSet>
#include <QWidget>
#include <QtDebug>
@ -36,6 +37,9 @@ public:
/// Returns the weak hash under which all local shared objects are registered.
static const WeakSharedObjectHash& getWeakHash() { return _weakHash; }
/// Returns a reference to the weak hash lock.
static QReadWriteLock& getWeakHashLock() { return _weakHashLock; }
Q_INVOKABLE SharedObject();
/// Returns the unique local ID for this object.
@ -85,6 +89,7 @@ private:
static int _lastID;
static WeakSharedObjectHash _weakHash;
static QReadWriteLock _weakHashLock;
};
/// Removes the null references from the supplied hash.