Skip to content
Permalink
Browse files

Fix crash when accessing QgsApplication::dataItemProviderRegistry()

without QgsApplication instance.

Also add test to ensure that calling QgsApplication::initQgis()
is safe to do without QgsApplication instance.
  • Loading branch information
nyalldawson committed Nov 6, 2017
1 parent c413f14 commit 2eff873e0e55bf1f7b95995020fa8cf91fa7e321
Showing with 29 additions and 2 deletions.
  1. +18 −2 src/core/qgsapplication.cpp
  2. +2 −0 src/core/qgsapplication.h
  3. +9 −0 tests/src/python/test_qgsnoapplication.py
@@ -112,6 +112,7 @@ const char *QgsApplication::QGIS_APPLICATION_NAME = "QGIS3";

QgsApplication::ApplicationMembers *QgsApplication::sApplicationMembers = nullptr;
QgsAuthManager *QgsApplication::sAuthManager = nullptr;
QgsDataItemProviderRegistry *QgsApplication::sDataItemProviderRegistry = nullptr;

QgsApplication::QgsApplication( int &argc, char **argv, bool GUIenabled, const QString &profileFolder, const QString &platformName )
: QApplication( argc, argv, GUIenabled )
@@ -871,7 +872,8 @@ void QgsApplication::initQgis()
// set the provider plugin path (this creates provider registry)
QgsProviderRegistry::instance( pluginPath() );

instance()->mDataItemProviderRegistry = new QgsDataItemProviderRegistry();
// create data item provider registry
( void )QgsApplication::dataItemProviderRegistry();

// create project instance if doesn't exist
QgsProject::instance();
@@ -1559,7 +1561,21 @@ QgsRasterRendererRegistry *QgsApplication::rasterRendererRegistry()

QgsDataItemProviderRegistry *QgsApplication::dataItemProviderRegistry()
{
return instance()->mDataItemProviderRegistry;
if ( instance() )
{
if ( !instance()->mDataItemProviderRegistry )
{
instance()->mDataItemProviderRegistry = new QgsDataItemProviderRegistry();
}
return instance()->mDataItemProviderRegistry;
}
else
{
// no QgsApplication instance
if ( !sDataItemProviderRegistry )
sDataItemProviderRegistry = new QgsDataItemProviderRegistry();
return sDataItemProviderRegistry;
}
}

QgsSvgCache *QgsApplication::svgCache()
@@ -749,6 +749,8 @@ class CORE_EXPORT QgsApplication : public QApplication
QMap<QString, QIcon> mIconCache;

QgsDataItemProviderRegistry *mDataItemProviderRegistry = nullptr;
// ... but in case QgsApplication is never instantiated (eg with custom designer widgets), we fall back to static instance
static QgsDataItemProviderRegistry *sDataItemProviderRegistry;

QgsAuthManager *mAuthManager = nullptr;
// ... but in case QgsApplication is never instantiated (eg with custom designer widgets), we fall back to static instance
@@ -56,6 +56,15 @@ def testNullRepresentation(self):
def testAuthManager(self):
self.assertTrue(QgsApplication.authManager())

def testDataItemProviderRegistry(self):
self.assertTrue(QgsApplication.dataItemProviderRegistry())

def testInit(self):
"""
Test calling QgsApplication.initQgis() without QgsApplication instance
"""
QgsApplication.initQgis()


if __name__ == '__main__':
unittest.main()

0 comments on commit 2eff873

Please sign in to comment.
You can’t perform that action at this time.