Skip to content

Commit 0512538

Browse files
author
Sandro Santilli
committedFeb 2, 2016
Ensure GDAL deinitialization runs after last possible use
Closes #14176
1 parent 9fb7819 commit 0512538

File tree

5 files changed

+40
-13
lines changed

5 files changed

+40
-13
lines changed
 

‎src/core/qgsapplication.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -876,6 +876,10 @@ void QgsApplication::exitQgis()
876876

877877
//delete all registered functions from expression engine (see above comment)
878878
QgsExpression::cleanRegisteredFunctions();
879+
880+
// tear-down GDAL/OGR
881+
OGRCleanupAll();
882+
GDALDestroyDriverManager();
879883
}
880884

881885
QString QgsApplication::showSettings()

‎src/providers/gdal/qgsgdalprovider.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3021,5 +3021,6 @@ QGISEXTERN QList<QPair<QString, QString> > *pyramidResamplingMethods()
30213021

30223022
QGISEXTERN void cleanupProvider()
30233023
{
3024-
GDALDestroyDriverManager();
3024+
// nothing to do here, QgsApplication takes care of
3025+
// calling GDALDestroyDriverManager()
30253026
}

‎src/providers/ogr/qgsogrconnpool.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,20 @@
1515

1616
#include "qgsogrconnpool.h"
1717

18-
QgsOgrConnPool QgsOgrConnPool::sInstance;
19-
bool QgsOgrConnPool::sInstanceDestroyed = false;
18+
QgsOgrConnPool* QgsOgrConnPool::mInstance = nullptr;
2019

20+
// static public
2121
QgsOgrConnPool* QgsOgrConnPool::instance()
2222
{
23-
return sInstanceDestroyed ? nullptr : &sInstance;
23+
if ( ! mInstance ) mInstance = new QgsOgrConnPool();
24+
return mInstance;
25+
}
26+
27+
// static public
28+
void QgsOgrConnPool::cleanupInstance()
29+
{
30+
delete mInstance;
31+
mInstance = nullptr;
2432
}
2533

2634
QgsOgrConnPool::QgsOgrConnPool() : QgsConnectionPool<QgsOgrConn*, QgsOgrConnPoolGroup>()
@@ -31,5 +39,4 @@ QgsOgrConnPool::QgsOgrConnPool() : QgsConnectionPool<QgsOgrConn*, QgsOgrConnPool
3139
QgsOgrConnPool::~QgsOgrConnPool()
3240
{
3341
QgsDebugCall;
34-
sInstanceDestroyed = true;
3542
}

‎src/providers/ogr/qgsogrconnpool.h

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,25 @@ class QgsOgrConnPoolGroup : public QObject, public QgsConnectionPoolGroup<QgsOgr
8686
class QgsOgrConnPool : public QgsConnectionPool<QgsOgrConn*, QgsOgrConnPoolGroup>
8787
{
8888
public:
89+
90+
// NOTE: first call to this function initializes the
91+
// singleton.
92+
// WARNING: concurrent call from multiple threads may result
93+
// in multiple instances being created, and memory
94+
// leaking at exit.
95+
//
8996
static QgsOgrConnPool* instance();
9097

98+
// Singleton cleanup
99+
//
100+
// Make sure nobody is using the instance before calling
101+
// this function.
102+
//
103+
// WARNING: concurrent call from multiple threads may result
104+
// in double-free of the instance.
105+
//
106+
static void cleanupInstance();
107+
91108
void ref( const QString& connInfo )
92109
{
93110
mMutex.lock();
@@ -118,14 +135,12 @@ class QgsOgrConnPool : public QgsConnectionPool<QgsOgrConn*, QgsOgrConnPoolGroup
118135

119136
static void refS( const QString &connInfo )
120137
{
121-
if ( instance() )
122-
instance()->ref( connInfo );
138+
instance()->ref( connInfo );
123139
}
124140

125141
static void unrefS( const QString &connInfo )
126142
{
127-
if ( instance() )
128-
instance()->unref( connInfo );
143+
instance()->unref( connInfo );
129144
}
130145

131146
protected:
@@ -134,9 +149,7 @@ class QgsOgrConnPool : public QgsConnectionPool<QgsOgrConn*, QgsOgrConnPoolGroup
134149
private:
135150
QgsOgrConnPool();
136151
~QgsOgrConnPool();
137-
138-
static QgsOgrConnPool sInstance;
139-
static bool sInstanceDestroyed;
152+
static QgsOgrConnPool *mInstance;
140153
};
141154

142155

‎src/providers/ogr/qgsogrprovider.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2857,5 +2857,7 @@ QGISEXTERN QgsVectorLayerImport::ImportError createEmptyLayer(
28572857

28582858
QGISEXTERN void cleanupProvider()
28592859
{
2860-
OGRCleanupAll();
2860+
QgsOgrConnPool::cleanupInstance();
2861+
// NOTE: QgsApplication takes care of
2862+
// calling OGRCleanupAll();
28612863
}

0 commit comments

Comments
 (0)
Please sign in to comment.