Skip to content

Commit

Permalink
Add thread safety protection for adding a layer to a layer store
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Dec 14, 2022
1 parent 8921e69 commit cb941b9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/core/qgsmaplayerstore.cpp
Expand Up @@ -18,6 +18,7 @@
#include "qgsmaplayerstore.h"
#include "qgsmaplayer.h"
#include "qgslogger.h"
#include "qgsthreadingutils.h"
#include <QList>

QgsMapLayerStore::QgsMapLayerStore( QObject *parent )
Expand Down Expand Up @@ -76,6 +77,9 @@ QList<QgsMapLayer *> QgsMapLayerStore::addMapLayers( const QList<QgsMapLayer *>
QgsDebugMsg( QStringLiteral( "Cannot add null layers" ) );
continue;
}

QGIS_CHECK_QOBJECT_THREAD_EQUALITY( myLayer );

// If the layer is already in the store but its validity has flipped to TRUE reset data source
if ( mMapLayers.contains( myLayer->id() ) && ! mMapLayers[myLayer->id()]->isValid() && myLayer->isValid() && myLayer->dataProvider() )
{
Expand Down
12 changes: 12 additions & 0 deletions src/core/qgsthreadingutils.h
Expand Up @@ -24,7 +24,11 @@
#include "qgsfeedback.h"

#include <QThread>
#if defined(QGISDEBUG) || defined(AGGRESSIVE_SAFE_MODE)
#include <QDebug>
#endif
#include <QSemaphore>
#include <QCoreApplication>
#include <memory>

#ifdef AGGRESSIVE_SAFE_MODE
Expand All @@ -43,6 +47,14 @@
#define QGIS_PROTECT_QOBJECT_THREAD_ACCESS_NON_FATAL do {} while(false);
#endif

#ifdef AGGRESSIVE_SAFE_MODE
#define QGIS_CHECK_QOBJECT_THREAD_EQUALITY(other) if ( other->thread() != thread() ) {qFatal( "%s", QStringLiteral("%2 (%1:%3) Object %4 is from a different thread than the object %5 lives in [0x%6 vs 0x%7]" ).arg( QString( __FILE__ ), QString( __FUNCTION__ ), QString::number( __LINE__ ), other->objectName(), objectName() ).arg( reinterpret_cast< qint64 >( QThread::currentThread() ), 0, 16 ).arg( reinterpret_cast< qint64 >( thread() ), 0, 16 ).toLocal8Bit().constData() ); }
#elif defined(QGISDEBUG)
#define QGIS_CHECK_QOBJECT_THREAD_EQUALITY(other) if ( other->thread() != thread() ) {qWarning() << QStringLiteral("%2 (%1:%3) Object %4 is from a different thread than the object %5 lives in [0x%6 vs 0x%7]" ).arg( QString( __FILE__ ), QString( __FUNCTION__ ), QString::number( __LINE__ ), other->objectName(), objectName() ).arg( reinterpret_cast< qint64 >( QThread::currentThread() ), 0, 16 ).arg( reinterpret_cast< qint64 >( thread() ), 0, 16 ).toLocal8Bit().constData(); }
#else
#define QGIS_CHECK_QOBJECT_THREAD_EQUALITY(other) do {} while(false);(void)other;
#endif


/**
* \ingroup core
Expand Down

0 comments on commit cb941b9

Please sign in to comment.