Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allocate OgrPool singleton on the heap #2754

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/core/qgsapplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
3 changes: 2 additions & 1 deletion src/providers/gdal/qgsgdalprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3021,5 +3021,6 @@ QGISEXTERN QList<QPair<QString, QString> > *pyramidResamplingMethods()

QGISEXTERN void cleanupProvider()
{
GDALDestroyDriverManager();
// nothing to do here, QgsApplication takes care of
// calling GDALDestroyDriverManager()
}
15 changes: 11 additions & 4 deletions src/providers/ogr/qgsogrconnpool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<QgsOgrConn*, QgsOgrConnPoolGroup>()
Expand All @@ -31,5 +39,4 @@ QgsOgrConnPool::QgsOgrConnPool() : QgsConnectionPool<QgsOgrConn*, QgsOgrConnPool
QgsOgrConnPool::~QgsOgrConnPool()
{
QgsDebugCall;
sInstanceDestroyed = true;
}
27 changes: 20 additions & 7 deletions src/providers/ogr/qgsogrconnpool.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,25 @@ class QgsOgrConnPoolGroup : public QObject, public QgsConnectionPoolGroup<QgsOgr
class QgsOgrConnPool : public QgsConnectionPool<QgsOgrConn*, QgsOgrConnPoolGroup>
{
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();
Expand Down Expand Up @@ -118,14 +135,12 @@ class QgsOgrConnPool : public QgsConnectionPool<QgsOgrConn*, QgsOgrConnPoolGroup

static void refS( const QString &connInfo )
{
if ( instance() )
instance()->ref( connInfo );
instance()->ref( connInfo );
}

static void unrefS( const QString &connInfo )
{
if ( instance() )
instance()->unref( connInfo );
instance()->unref( connInfo );
}

protected:
Expand All @@ -134,9 +149,7 @@ class QgsOgrConnPool : public QgsConnectionPool<QgsOgrConn*, QgsOgrConnPoolGroup
private:
QgsOgrConnPool();
~QgsOgrConnPool();

static QgsOgrConnPool sInstance;
static bool sInstanceDestroyed;
static QgsOgrConnPool *mInstance;
};


Expand Down
4 changes: 3 additions & 1 deletion src/providers/ogr/qgsogrprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2875,5 +2875,7 @@ QGISEXTERN QgsVectorLayerImport::ImportError createEmptyLayer(

QGISEXTERN void cleanupProvider()
{
OGRCleanupAll();
QgsOgrConnPool::cleanupInstance();
// NOTE: QgsApplication takes care of
// calling OGRCleanupAll();
}