Skip to content

Commit

Permalink
Fix crash when accessing auth manager when no QgsApplication is avail…
Browse files Browse the repository at this point in the history
…able
  • Loading branch information
nyalldawson committed Nov 6, 2017
1 parent 7cfbb6f commit c413f14
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/core/qgsapplication.cpp
Expand Up @@ -111,6 +111,7 @@ const char *QgsApplication::QGIS_ORGANIZATION_DOMAIN = "qgis.org";
const char *QgsApplication::QGIS_APPLICATION_NAME = "QGIS3";

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

QgsApplication::QgsApplication( int &argc, char **argv, bool GUIenabled, const QString &profileFolder, const QString &platformName )
: QApplication( argc, argv, GUIenabled )
Expand Down Expand Up @@ -889,11 +890,21 @@ void QgsApplication::initQgis()

QgsAuthManager *QgsApplication::authManager()
{
if ( ! instance()->mAuthManager )
if ( instance() )
{
if ( !instance()->mAuthManager )
{
instance()->mAuthManager = QgsAuthManager::instance();
}
return instance()->mAuthManager;
}
else
{
instance()->mAuthManager = QgsAuthManager::instance();
// no QgsApplication instance
if ( !sAuthManager )
sAuthManager = QgsAuthManager::instance();
return sAuthManager;
}
return instance()->mAuthManager;
}


Expand Down
3 changes: 3 additions & 0 deletions src/core/qgsapplication.h
Expand Up @@ -749,7 +749,10 @@ class CORE_EXPORT QgsApplication : public QApplication
QMap<QString, QIcon> mIconCache;

QgsDataItemProviderRegistry *mDataItemProviderRegistry = nullptr;

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

struct ApplicationMembers
{
Expand Down
3 changes: 3 additions & 0 deletions tests/src/python/test_qgsnoapplication.py
Expand Up @@ -53,6 +53,9 @@ def testNullRepresentation(self):
QgsApplication.setNullRepresentation(nr)
self.assertEqual(QgsApplication.nullRepresentation(), nr)

def testAuthManager(self):
self.assertTrue(QgsApplication.authManager())


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

0 comments on commit c413f14

Please sign in to comment.