Skip to content

Commit c2fb5e1

Browse files
committedApr 28, 2015
Revert "Streamline singleton behavior"
This reverts commit b4a8547. The template based approach was not cross-platform friendly. When cross-compiling for Android it caused a new instance of every singleton per shared library. Mostly using the magic statics pattern instead now: See http://stackoverflow.com/a/11711991/2319028 Important things to remember for crash on exit: * QgsNetworkAccessManager needs to die before QApplication * QgsMapLayerRegistry needs to be emptied before QgsProviderRegistry goes away And finally: DON'T USE SINGLETONS! They are for "one and only one" and not for "happens to be only one" situations.
1 parent 96560c8 commit c2fb5e1

18 files changed

+68
-99
lines changed
 

‎src/app/qgisapp.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,8 +1000,6 @@ QgisApp::~QgisApp()
10001000
delete QgsProject::instance();
10011001

10021002
delete mPythonUtils;
1003-
1004-
QgsMapLayerStyleGuiUtils::cleanup();
10051003
}
10061004

10071005
void QgisApp::dragEnterEvent( QDragEnterEvent *event )

‎src/app/qgsmaplayerstyleguiutils.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@
2525
#include "qgsmaplayerstylemanager.h"
2626

2727

28+
QgsMapLayerStyleGuiUtils* QgsMapLayerStyleGuiUtils::instance()
29+
{
30+
static QgsMapLayerStyleGuiUtils sInstance;
31+
return &sInstance;
32+
}
33+
2834
QAction* QgsMapLayerStyleGuiUtils::actionAddStyle( QgsMapLayer* layer, QObject* parent )
2935
{
3036
QAction* a = new QAction( tr( "Add" ), parent );

‎src/app/qgsmaplayerstyleguiutils.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,18 @@
1818

1919
#include <QObject>
2020

21-
#include "qgssingleton.h"
22-
2321
class QgsMapLayer;
2422

2523
class QAction;
2624
class QMenu;
2725

2826
/** Various GUI utility functions for dealing with map layer's style manager */
29-
class QgsMapLayerStyleGuiUtils : public QObject, public QgsSingleton<QgsMapLayerStyleGuiUtils>
27+
class QgsMapLayerStyleGuiUtils : public QObject
3028
{
3129
Q_OBJECT
32-
public:
3330

31+
public:
32+
static QgsMapLayerStyleGuiUtils* instance();
3433
QAction* actionAddStyle( QgsMapLayer* layer, QObject* parent = 0 );
3534
QAction* actionRemoveStyle( QgsMapLayer* layer, QObject* parent = 0 );
3635
QAction* actionRenameStyle( QgsMapLayer* layer, QObject* parent = 0 );

‎src/core/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,6 @@ SET(QGIS_CORE_HDRS
548548
qgsscalecalculator.h
549549
qgsscaleutils.h
550550
qgssimplifymethod.h
551-
qgssingleton.h
552551
qgssnapper.h
553552
qgssnappingutils.h
554553
qgsspatialindex.h

‎src/core/effects/qgspainteffectregistry.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@ QgsPaintEffectRegistry::~QgsPaintEffectRegistry()
6060
mMetadata.clear();
6161
}
6262

63+
QgsPaintEffectRegistry* QgsPaintEffectRegistry::instance()
64+
{
65+
static QgsPaintEffectRegistry sInstance;
66+
return &sInstance;
67+
}
68+
6369
QgsPaintEffectAbstractMetadata *QgsPaintEffectRegistry::effectMetadata( const QString &name ) const
6470
{
6571
if ( mMetadata.contains( name ) )

‎src/core/effects/qgspainteffectregistry.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
#ifndef QGSPAINTEFFECTREGISTRY_H
1717
#define QGSPAINTEFFECTREGISTRY_H
1818

19-
#include "qgssingleton.h"
2019
#include "qgis.h"
2120
#include <QDomElement>
2221
#include <QDomDocument>
@@ -152,9 +151,10 @@ class CORE_EXPORT QgsPaintEffectMetadata : public QgsPaintEffectAbstractMetadata
152151
*
153152
* \note Added in version 2.9
154153
*/
155-
class CORE_EXPORT QgsPaintEffectRegistry : public QgsSingleton<QgsPaintEffectRegistry>
154+
class CORE_EXPORT QgsPaintEffectRegistry
156155
{
157156
public:
157+
static QgsPaintEffectRegistry* instance();
158158

159159
/** Returns the metadata for a specific effect.
160160
* @param name unique string name for paint effect class
@@ -194,8 +194,6 @@ class CORE_EXPORT QgsPaintEffectRegistry : public QgsSingleton<QgsPaintEffectReg
194194
~QgsPaintEffectRegistry();
195195

196196
QMap<QString, QgsPaintEffectAbstractMetadata*> mMetadata;
197-
198-
friend class QgsSingleton<QgsPaintEffectRegistry>; // Let QgsSingleton access private constructor
199197
};
200198

201199
#endif //QGSPAINTEFFECTREGISTRY_H

‎src/core/qgsapplication.cpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -623,13 +623,6 @@ void QgsApplication::initQgis()
623623

624624
void QgsApplication::exitQgis()
625625
{
626-
// Cleanup known singletons
627-
QgsMapLayerRegistry::cleanup();
628-
QgsNetworkAccessManager::cleanup();
629-
QgsCoordinateTransformCache::cleanup();
630-
QgsDataItemProviderRegistry::cleanup();
631-
632-
// Cleanup providers
633626
delete QgsProviderRegistry::instance();
634627
}
635628

‎src/core/qgscrscache.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,20 @@
1818
#include "qgscrscache.h"
1919
#include "qgscoordinatetransform.h"
2020

21+
22+
QgsCoordinateTransformCache* QgsCoordinateTransformCache::instance()
23+
{
24+
static QgsCoordinateTransformCache mInstance;
25+
return &mInstance;
26+
}
27+
2128
QgsCoordinateTransformCache::~QgsCoordinateTransformCache()
2229
{
2330
QHash< QPair< QString, QString >, QgsCoordinateTransform* >::const_iterator tIt = mTransforms.constBegin();
2431
for ( ; tIt != mTransforms.constEnd(); ++tIt )
2532
{
2633
delete tIt.value();
2734
}
28-
29-
mTransforms.clear();
3035
}
3136

3237
const QgsCoordinateTransform* QgsCoordinateTransformCache::transform( const QString& srcAuthId, const QString& destAuthId, int srcDatumTransform, int destDatumTransform )

‎src/core/qgscrscache.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,16 @@
1919
#define QGSCRSCACHE_H
2020

2121
#include "qgscoordinatereferencesystem.h"
22-
#include "qgssingleton.h"
2322
#include <QHash>
2423

2524
class QgsCoordinateTransform;
2625

2726
/**Cache coordinate transform by authid of source/dest transformation to avoid the
2827
overhead of initialisation for each redraw*/
29-
class CORE_EXPORT QgsCoordinateTransformCache : public QgsSingleton<QgsCoordinateTransformCache>
28+
class CORE_EXPORT QgsCoordinateTransformCache
3029
{
3130
public:
31+
static QgsCoordinateTransformCache* instance();
3232
~QgsCoordinateTransformCache();
3333
/**Returns coordinate transformation. Cache keeps ownership
3434
@param srcAuthId auth id string of source crs
@@ -41,6 +41,7 @@ class CORE_EXPORT QgsCoordinateTransformCache : public QgsSingleton<QgsCoordinat
4141
void invalidateCrs( const QString& crsAuthId );
4242

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

‎src/core/qgsdataitemproviderregistry.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,12 @@ QgsDataItemProviderRegistry::QgsDataItemProviderRegistry()
7979
}
8080
}
8181

82+
QgsDataItemProviderRegistry* QgsDataItemProviderRegistry::instance()
83+
{
84+
static QgsDataItemProviderRegistry sInstance;
85+
return &sInstance;
86+
}
87+
8288
QgsDataItemProviderRegistry::~QgsDataItemProviderRegistry()
8389
{
8490
qDeleteAll( mProviders );

‎src/core/qgsdataitemproviderregistry.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
#ifndef QGSDATAITEMPROVIDERREGISTRY_H
1717
#define QGSDATAITEMPROVIDERREGISTRY_H
1818

19-
#include "qgssingleton.h"
20-
2119
#include <QList>
2220

2321
class QgsDataItemProvider;
@@ -28,9 +26,11 @@ class QgsDataItemProvider;
2826
*
2927
* @note added in 2.10
3028
*/
31-
class CORE_EXPORT QgsDataItemProviderRegistry : public QgsSingleton<QgsDataItemProviderRegistry>
29+
class CORE_EXPORT QgsDataItemProviderRegistry
3230
{
3331
public:
32+
static QgsDataItemProviderRegistry* instance();
33+
3434
~QgsDataItemProviderRegistry();
3535

3636
//! Get list of available providers
@@ -45,8 +45,6 @@ class CORE_EXPORT QgsDataItemProviderRegistry : public QgsSingleton<QgsDataItemP
4545
private:
4646
QgsDataItemProviderRegistry();
4747

48-
friend class QgsSingleton<QgsDataItemProviderRegistry>; // Let QgsSingleton access private constructor
49-
5048
//! available providers. this class owns the pointers
5149
QList<QgsDataItemProvider*> mProviders;
5250
};

‎src/core/qgsmaplayerregistry.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@
1919
#include "qgsmaplayer.h"
2020
#include "qgslogger.h"
2121

22+
//
23+
// Static calls to enforce singleton behaviour
24+
//
25+
QgsMapLayerRegistry *QgsMapLayerRegistry::instance()
26+
{
27+
static QgsMapLayerRegistry sInstance;
28+
return &sInstance;
29+
}
30+
2231
//
2332
// Main class begins now...
2433
//

‎src/core/qgsmaplayerregistry.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,21 @@
2323
#include <QSet>
2424
#include <QObject>
2525
#include <QStringList>
26-
27-
#include "qgssingleton.h"
2826
class QString;
2927
class QgsMapLayer;
3028

3129
/** \ingroup core
3230
* This class tracks map layers that are currently loaded and provides
3331
* a means to fetch a pointer to a map layer and delete it.
3432
*/
35-
class CORE_EXPORT QgsMapLayerRegistry : public QObject, public QgsSingleton<QgsMapLayerRegistry>
33+
class CORE_EXPORT QgsMapLayerRegistry : public QObject
3634
{
3735
Q_OBJECT
3836

3937
public:
38+
//! Returns the instance pointer, creating the object on the first call
39+
static QgsMapLayerRegistry * instance();
40+
4041
//! Return the number of registered layers.
4142
int count();
4243

@@ -242,8 +243,6 @@ class CORE_EXPORT QgsMapLayerRegistry : public QObject, public QgsSingleton<QgsM
242243

243244
QMap<QString, QgsMapLayer*> mMapLayers;
244245
QSet<QgsMapLayer*> mOwnedLayers;
245-
246-
friend class QgsSingleton<QgsMapLayerRegistry>; // Let QgsSingleton access private constructor
247246
}; // class QgsMapLayerRegistry
248247

249248
#endif //QgsMapLayerRegistry_H

‎src/core/qgsnetworkaccessmanager.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,15 @@ class QgsNetworkProxyFactory : public QNetworkProxyFactory
8686
}
8787
};
8888

89+
//
90+
// Static calls to enforce singleton behaviour
91+
//
92+
QgsNetworkAccessManager* QgsNetworkAccessManager::instance()
93+
{
94+
static QgsNetworkAccessManager* sInstance( new QgsNetworkAccessManager( QApplication::instance() ) );
95+
return sInstance;
96+
}
97+
8998
QgsNetworkAccessManager::QgsNetworkAccessManager( QObject *parent )
9099
: QNetworkAccessManager( parent )
91100
, mUseSystemProxy( false )

‎src/core/qgsnetworkaccessmanager.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@
2424
#include <QNetworkProxy>
2525
#include <QNetworkRequest>
2626

27-
#include "qgssingleton.h"
28-
2927
/*
3028
* \class QgsNetworkAccessManager
3129
* \brief network access manager for QGIS
@@ -43,11 +41,15 @@
4341
* that the fallback proxy should not be used for, then no proxy will be used.
4442
*
4543
*/
46-
class CORE_EXPORT QgsNetworkAccessManager : public QNetworkAccessManager, public QgsSingleton<QgsNetworkAccessManager>
44+
class CORE_EXPORT QgsNetworkAccessManager : public QNetworkAccessManager
4745
{
4846
Q_OBJECT
4947

5048
public:
49+
//! returns a pointer to the single instance
50+
// and creates that instance on the first call.
51+
static QgsNetworkAccessManager* instance();
52+
5153
QgsNetworkAccessManager( QObject *parent = 0 );
5254

5355
//! destructor

‎src/core/qgsproviderregistry.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "qgsmessagelog.h"
3030
#include "qgsprovidermetadata.h"
3131
#include "qgsvectorlayer.h"
32+
#include "qgsmaplayerregistry.h"
3233

3334

3435
// typedefs for provider plugin functions of interest
@@ -204,11 +205,13 @@ typedef void cleanupProviderFunction_t();
204205

205206
QgsProviderRegistry::~QgsProviderRegistry()
206207
{
208+
QgsMapLayerRegistry::instance()->removeAllMapLayers();
209+
207210
Providers::const_iterator it = mProviders.begin();
208211

209212
while ( it != mProviders.end() )
210213
{
211-
QgsDebugMsg( QString( "cleanup: %1" ).arg( it->first ) );
214+
QgsDebugMsg( QString( "cleanup:%1" ).arg( it->first ) );
212215
QString lib = it->second->library();
213216
QLibrary myLib( lib );
214217
if ( myLib.isLoaded() )
@@ -405,7 +408,7 @@ QWidget* QgsProviderRegistry::selectWidget( const QString & providerKey,
405408

406409
#if QT_VERSION >= 0x050000
407410
QFunctionPointer QgsProviderRegistry::function( QString const & providerKey,
408-
QString const & functionName )
411+
QString const & functionName )
409412
{
410413
QLibrary myLib( library( providerKey ) );
411414

‎src/core/qgssingleton.h

Lines changed: 0 additions & 55 deletions
This file was deleted.

0 commit comments

Comments
 (0)