diff --git a/src/core/qgsapplication.cpp b/src/core/qgsapplication.cpp index a65d7a539add..19d428e8457e 100644 --- a/src/core/qgsapplication.cpp +++ b/src/core/qgsapplication.cpp @@ -876,6 +876,10 @@ void QgsApplication::exitQgis() //delete all registered functions from expression engine (see above comment) QgsExpression::cleanRegisteredFunctions(); + + // tear-down GDAL/OGR + OGRCleanupAll(); + GDALDestroyDriverManager(); } QString QgsApplication::showSettings() diff --git a/src/providers/gdal/qgsgdalprovider.cpp b/src/providers/gdal/qgsgdalprovider.cpp index bc2330afde05..ca97207532a8 100644 --- a/src/providers/gdal/qgsgdalprovider.cpp +++ b/src/providers/gdal/qgsgdalprovider.cpp @@ -3021,5 +3021,6 @@ QGISEXTERN QList > *pyramidResamplingMethods() QGISEXTERN void cleanupProvider() { - GDALDestroyDriverManager(); + // nothing to do here, QgsApplication takes care of + // calling GDALDestroyDriverManager() } diff --git a/src/providers/ogr/qgsogrconnpool.cpp b/src/providers/ogr/qgsogrconnpool.cpp index 46f99682a245..7ab95e67cbc8 100644 --- a/src/providers/ogr/qgsogrconnpool.cpp +++ b/src/providers/ogr/qgsogrconnpool.cpp @@ -15,12 +15,20 @@ #include "qgsogrconnpool.h" -QgsOgrConnPool QgsOgrConnPool::sInstance; -bool QgsOgrConnPool::sInstanceDestroyed = false; +QgsOgrConnPool* QgsOgrConnPool::mInstance = nullptr; +// static public QgsOgrConnPool* QgsOgrConnPool::instance() { - return sInstanceDestroyed ? nullptr : &sInstance; + if ( ! mInstance ) mInstance = new QgsOgrConnPool(); + return mInstance; +} + +// static public +void QgsOgrConnPool::cleanupInstance() +{ + delete mInstance; + mInstance = nullptr; } QgsOgrConnPool::QgsOgrConnPool() : QgsConnectionPool() @@ -31,5 +39,4 @@ QgsOgrConnPool::QgsOgrConnPool() : QgsConnectionPool { public: + + // NOTE: first call to this function initializes the + // singleton. + // WARNING: concurrent call from multiple threads may result + // in multiple instances being created, and memory + // leaking at exit. + // static QgsOgrConnPool* instance(); + // Singleton cleanup + // + // Make sure nobody is using the instance before calling + // this function. + // + // WARNING: concurrent call from multiple threads may result + // in double-free of the instance. + // + static void cleanupInstance(); + void ref( const QString& connInfo ) { mMutex.lock(); @@ -118,14 +135,12 @@ class QgsOgrConnPool : public QgsConnectionPoolref( connInfo ); + instance()->ref( connInfo ); } static void unrefS( const QString &connInfo ) { - if ( instance() ) - instance()->unref( connInfo ); + instance()->unref( connInfo ); } protected: @@ -134,9 +149,7 @@ class QgsOgrConnPool : public QgsConnectionPool