Skip to content

Commit 581d0d3

Browse files
committedNov 15, 2017
[Spatialite provider] Properly destroy QgsSpatiaLiteConnPool singleton at provider unloading. Should fix frequent crash in PyQgsServerWMS on Travis (fixes #17447)
1 parent 7fa11c8 commit 581d0d3

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed
 

‎src/providers/spatialite/qgsspatialiteconnpool.cpp

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,30 @@
1313
* *
1414
***************************************************************************/
1515

16+
#include <QMutex>
17+
#include <QMutexLocker>
18+
1619
#include "qgsspatialiteconnpool.h"
1720

18-
QgsSpatiaLiteConnPool QgsSpatiaLiteConnPool::sInstance;
21+
QgsSpatiaLiteConnPool *QgsSpatiaLiteConnPool::sInstance = nullptr;
1922

2023
QgsSpatiaLiteConnPool *QgsSpatiaLiteConnPool::instance()
2124
{
22-
return &sInstance;
25+
if ( ! sInstance )
26+
{
27+
static QMutex sMutex;
28+
QMutexLocker locker( &sMutex );
29+
if ( ! sInstance )
30+
{
31+
sInstance = new QgsSpatiaLiteConnPool();
32+
}
33+
}
34+
return sInstance;
35+
}
36+
37+
// static public
38+
void QgsSpatiaLiteConnPool::cleanupInstance()
39+
{
40+
delete sInstance;
41+
sInstance = nullptr;
2342
}

‎src/providers/spatialite/qgsspatialiteconnpool.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,19 @@ class QgsSpatiaLiteConnPoolGroup : public QObject, public QgsConnectionPoolGroup
6868
//! SpatiaLite connection pool - singleton
6969
class QgsSpatiaLiteConnPool : public QgsConnectionPool<QgsSqliteHandle *, QgsSpatiaLiteConnPoolGroup>
7070
{
71-
static QgsSpatiaLiteConnPool sInstance;
71+
static QgsSpatiaLiteConnPool *sInstance;
7272
public:
7373
static QgsSpatiaLiteConnPool *instance();
74+
75+
// Singleton cleanup
76+
//
77+
// Make sure nobody is using the instance before calling
78+
// this function.
79+
//
80+
// WARNING: concurrent call from multiple threads may result
81+
// in double-free of the instance.
82+
//
83+
static void cleanupInstance();
7484
};
7585

7686

‎src/providers/spatialite/qgsspatialiteprovider.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5900,6 +5900,7 @@ QGISEXTERN QString getStyleById( const QString &uri, QString styleId, QString &e
59005900

59015901
QGISEXTERN void cleanupProvider()
59025902
{
5903+
QgsSpatiaLiteConnPool::cleanupInstance();
59035904
QgsSqliteHandle::closeAll();
59045905
}
59055906

1 commit comments

Comments
 (1)

pblottiere commented on Nov 16, 2017

@pblottiere
Member

Thank you @rouault! WMS tests seem to be stabilized now 🎉

Please sign in to comment.