Skip to content

Commit

Permalink
Streamline singleton behavior in a class QgsSingleton, take two
Browse files Browse the repository at this point in the history
delete provider registry after map layer registry to avoid crashes
  • Loading branch information
m-kuhn committed Nov 24, 2014
1 parent e61d27e commit b4a8547
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 55 deletions.
1 change: 1 addition & 0 deletions src/core/CMakeLists.txt
Expand Up @@ -557,6 +557,7 @@ SET(QGIS_CORE_HDRS
qgssimplifymethod.h
qgssnapper.h
qgsspatialindex.h
qgssingleton.h
qgstolerance.h
qgsvectordataprovider.h
qgsvectorlayercache.h
Expand Down
15 changes: 9 additions & 6 deletions src/core/qgsapplication.cpp
Expand Up @@ -14,12 +14,13 @@
***************************************************************************/

#include "qgsapplication.h"
#include "qgscrscache.h"
#include "qgsexception.h"
#include "qgsgeometry.h"
#include "qgslogger.h"
#include "qgsmaplayerregistry.h"
#include "qgsproviderregistry.h"
#include "qgsnetworkaccessmanager.h"
#include "qgsexception.h"
#include "qgsgeometry.h"
#include "qgsproviderregistry.h"

#include <QDir>
#include <QFile>
Expand Down Expand Up @@ -607,11 +608,13 @@ void QgsApplication::initQgis()

void QgsApplication::exitQgis()
{
delete QgsMapLayerRegistry::instance();
// Cleanup known singletons
QgsMapLayerRegistry::cleanup();
QgsNetworkAccessManager::cleanup();
QgsCoordinateTransformCache::cleanup();

// Cleanup providers
delete QgsProviderRegistry::instance();

delete QgsNetworkAccessManager::instance();
}

QString QgsApplication::showSettings()
Expand Down
9 changes: 2 additions & 7 deletions src/core/qgscrscache.cpp
Expand Up @@ -18,20 +18,15 @@
#include "qgscrscache.h"
#include "qgscoordinatetransform.h"


QgsCoordinateTransformCache* QgsCoordinateTransformCache::instance()
{
static QgsCoordinateTransformCache mInstance;
return &mInstance;
}

QgsCoordinateTransformCache::~QgsCoordinateTransformCache()
{
QHash< QPair< QString, QString >, QgsCoordinateTransform* >::const_iterator tIt = mTransforms.constBegin();
for ( ; tIt != mTransforms.constEnd(); ++tIt )
{
delete tIt.value();
}

mTransforms.clear();
}

const QgsCoordinateTransform* QgsCoordinateTransformCache::transform( const QString& srcAuthId, const QString& destAuthId, int srcDatumTransform, int destDatumTransform )
Expand Down
5 changes: 2 additions & 3 deletions src/core/qgscrscache.h
Expand Up @@ -19,16 +19,16 @@
#define QGSCRSCACHE_H

#include "qgscoordinatereferencesystem.h"
#include "qgssingleton.h"
#include <QHash>

class QgsCoordinateTransform;

/**Cache coordinate transform by authid of source/dest transformation to avoid the
overhead of initialisation for each redraw*/
class CORE_EXPORT QgsCoordinateTransformCache
class CORE_EXPORT QgsCoordinateTransformCache : public QgsSingleton<QgsCoordinateTransformCache>
{
public:
static QgsCoordinateTransformCache* instance();
~QgsCoordinateTransformCache();
/**Returns coordinate transformation. Cache keeps ownership
@param srcAuthId auth id string of source crs
Expand All @@ -41,7 +41,6 @@ class CORE_EXPORT QgsCoordinateTransformCache
void invalidateCrs( const QString& crsAuthId );

private:
static QgsCoordinateTransformCache* mInstance;
QMultiHash< QPair< QString, QString >, QgsCoordinateTransform* > mTransforms; //same auth_id pairs might have different datum transformations
};

Expand Down
13 changes: 0 additions & 13 deletions src/core/qgsmaplayerregistry.cpp
Expand Up @@ -19,19 +19,6 @@
#include "qgsmaplayer.h"
#include "qgslogger.h"

//
// Static calls to enforce singleton behaviour
//
QgsMapLayerRegistry *QgsMapLayerRegistry::mInstance = 0;
QgsMapLayerRegistry *QgsMapLayerRegistry::instance()
{
if ( mInstance == 0 )
{
mInstance = new QgsMapLayerRegistry();
}
return mInstance;
}

//
// Main class begins now...
//
Expand Down
10 changes: 5 additions & 5 deletions src/core/qgsmaplayerregistry.h
Expand Up @@ -23,21 +23,20 @@
#include <QSet>
#include <QObject>
#include <QStringList>

#include "qgssingleton.h"
class QString;
class QgsMapLayer;

/** \ingroup core
* This class tracks map layers that are currently loaded and provides
* a means to fetch a pointer to a map layer and delete it.
*/
class CORE_EXPORT QgsMapLayerRegistry : public QObject
class CORE_EXPORT QgsMapLayerRegistry : public QObject, public QgsSingleton<QgsMapLayerRegistry>
{
Q_OBJECT

public:
//! Returns the instance pointer, creating the object on the first call
static QgsMapLayerRegistry * instance();

//! Return the number of registered layers.
int count();

Expand Down Expand Up @@ -239,9 +238,10 @@ class CORE_EXPORT QgsMapLayerRegistry : public QObject
//! private singleton constructor
QgsMapLayerRegistry( QObject * parent = 0 );

static QgsMapLayerRegistry *mInstance;
QMap<QString, QgsMapLayer*> mMapLayers;
QSet<QgsMapLayer*> mOwnedLayers;

friend class QgsSingleton; // Let QgsSingleton access private constructor
}; // class QgsMapLayerRegistry

#endif //QgsMapLayerRegistry_H
Expand Down
13 changes: 0 additions & 13 deletions src/core/qgsnetworkaccessmanager.cpp
Expand Up @@ -86,19 +86,6 @@ class QgsNetworkProxyFactory : public QNetworkProxyFactory
}
};

//
// Static calls to enforce singleton behaviour
//
QgsNetworkAccessManager* QgsNetworkAccessManager::sInstance = 0;
QgsNetworkAccessManager* QgsNetworkAccessManager::instance()
{
if ( sInstance == 0 )
{
sInstance = new QgsNetworkAccessManager();
}
return sInstance;
}

QgsNetworkAccessManager::QgsNetworkAccessManager( QObject *parent )
: QNetworkAccessManager( parent )
, mUseSystemProxy( false )
Expand Down
9 changes: 3 additions & 6 deletions src/core/qgsnetworkaccessmanager.h
Expand Up @@ -24,6 +24,8 @@
#include <QNetworkProxy>
#include <QNetworkRequest>

#include "qgssingleton.h"

/*
* \class QgsNetworkAccessManager
* \brief network access manager for QGIS
Expand All @@ -41,15 +43,11 @@
* that the fallback proxy should not be used for, then no proxy will be used.
*
*/
class CORE_EXPORT QgsNetworkAccessManager : public QNetworkAccessManager
class CORE_EXPORT QgsNetworkAccessManager : public QNetworkAccessManager, public QgsSingleton<QgsNetworkAccessManager>
{
Q_OBJECT

public:
//! returns a pointer to the single instance
// and creates that instance on the first call.
static QgsNetworkAccessManager* instance();

QgsNetworkAccessManager( QObject *parent = 0 );

//! destructor
Expand Down Expand Up @@ -100,7 +98,6 @@ class CORE_EXPORT QgsNetworkAccessManager : public QNetworkAccessManager
QNetworkProxy mFallbackProxy;
QStringList mExcludedURLs;
bool mUseSystemProxy;
static QgsNetworkAccessManager* sInstance;
};

#endif // QGSNETWORKACCESSMANAGER_H
Expand Down
4 changes: 2 additions & 2 deletions src/core/qgsproviderregistry.cpp
Expand Up @@ -208,7 +208,7 @@ QgsProviderRegistry::~QgsProviderRegistry()

while ( it != mProviders.end() )
{
QgsDebugMsg( QString( "cleanup:%1" ).arg( it->first ) );
QgsDebugMsg( QString( "cleanup: %1" ).arg( it->first ) );
QString lib = it->second->library();
QLibrary myLib( lib );
if ( myLib.isLoaded() )
Expand Down Expand Up @@ -405,7 +405,7 @@ QWidget* QgsProviderRegistry::selectWidget( const QString & providerKey,

#if QT_VERSION >= 0x050000
QFunctionPointer QgsProviderRegistry::function( QString const & providerKey,
QString const & functionName )
QString const & functionName )
{
QLibrary myLib( library( providerKey ) );

Expand Down
57 changes: 57 additions & 0 deletions src/core/qgssingleton.h
@@ -0,0 +1,57 @@
/***************************************************************************
qgssingleton.h
--------------------------------------
Date : 24.11.2014
Copyright : (C) 2014 Matthias Kuhn
Email : matthias dot kuhn at gmx dot ch
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#ifndef QGSSINGLETON_H
#define QGSSINGLETON_H

template <typename T>
class QgsSingleton
{
public:
static T* instance()
{
if ( sInstance == 0 )
{
sInstance = createInstance();
}
return sInstance;
}

static void cleanup()
{
delete sInstance;
sInstance = 0;
}

protected:
virtual ~QgsSingleton() {}

explicit QgsSingleton()
{
Q_ASSERT( sInstance == 0 );
sInstance = static_cast<T*>( this );
}

private:
static T* sInstance;
static T* createInstance()
{
return new T;
}
};

template <typename T> T* QgsSingleton<T>::sInstance = 0;

#endif // QGSSINGLETON_H

0 comments on commit b4a8547

Please sign in to comment.