Skip to content

Commit c413f14

Browse files
committedNov 6, 2017
Fix crash when accessing auth manager when no QgsApplication is available
1 parent 7cfbb6f commit c413f14

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed
 

‎src/core/qgsapplication.cpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ const char *QgsApplication::QGIS_ORGANIZATION_DOMAIN = "qgis.org";
111111
const char *QgsApplication::QGIS_APPLICATION_NAME = "QGIS3";
112112

113113
QgsApplication::ApplicationMembers *QgsApplication::sApplicationMembers = nullptr;
114+
QgsAuthManager *QgsApplication::sAuthManager = nullptr;
114115

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

890891
QgsAuthManager *QgsApplication::authManager()
891892
{
892-
if ( ! instance()->mAuthManager )
893+
if ( instance() )
894+
{
895+
if ( !instance()->mAuthManager )
896+
{
897+
instance()->mAuthManager = QgsAuthManager::instance();
898+
}
899+
return instance()->mAuthManager;
900+
}
901+
else
893902
{
894-
instance()->mAuthManager = QgsAuthManager::instance();
903+
// no QgsApplication instance
904+
if ( !sAuthManager )
905+
sAuthManager = QgsAuthManager::instance();
906+
return sAuthManager;
895907
}
896-
return instance()->mAuthManager;
897908
}
898909

899910

‎src/core/qgsapplication.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -749,7 +749,10 @@ class CORE_EXPORT QgsApplication : public QApplication
749749
QMap<QString, QIcon> mIconCache;
750750

751751
QgsDataItemProviderRegistry *mDataItemProviderRegistry = nullptr;
752+
752753
QgsAuthManager *mAuthManager = nullptr;
754+
// ... but in case QgsApplication is never instantiated (eg with custom designer widgets), we fall back to static instance
755+
static QgsAuthManager *sAuthManager;
753756

754757
struct ApplicationMembers
755758
{

‎tests/src/python/test_qgsnoapplication.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ def testNullRepresentation(self):
5353
QgsApplication.setNullRepresentation(nr)
5454
self.assertEqual(QgsApplication.nullRepresentation(), nr)
5555

56+
def testAuthManager(self):
57+
self.assertTrue(QgsApplication.authManager())
58+
5659

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

0 commit comments

Comments
 (0)
Please sign in to comment.