Skip to content

Commit 95f880a

Browse files
committed
Fix occasional crash on exit because of statis network access manager
see https://bugreports.qt-project.org/browse/QTBUG-25789
1 parent f3eeed9 commit 95f880a

File tree

4 files changed

+19
-8
lines changed

4 files changed

+19
-8
lines changed

src/core/qgsapplication.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "qgslogger.h"
1818
#include "qgsmaplayerregistry.h"
1919
#include "qgsproviderregistry.h"
20+
#include "qgsnetworkaccessmanager.h"
2021
#include "qgsexception.h"
2122
#include "qgsgeometry.h"
2223

@@ -609,6 +610,8 @@ void QgsApplication::exitQgis()
609610
delete QgsMapLayerRegistry::instance();
610611

611612
delete QgsProviderRegistry::instance();
613+
614+
delete QgsNetworkAccessManager::instance();
612615
}
613616

614617
QString QgsApplication::showSettings()

src/core/qgsnetworkaccessmanager.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,17 @@ class QgsNetworkProxyFactory : public QNetworkProxyFactory
8686
}
8787
};
8888

89-
QgsNetworkAccessManager *QgsNetworkAccessManager::instance()
89+
//
90+
// Static calls to enforce singleton behaviour
91+
//
92+
QgsNetworkAccessManager* QgsNetworkAccessManager::sInstance = 0;
93+
QgsNetworkAccessManager* QgsNetworkAccessManager::instance()
9094
{
91-
static QgsNetworkAccessManager sInstance;
92-
return &sInstance;
95+
if ( sInstance == 0 )
96+
{
97+
sInstance = new QgsNetworkAccessManager();
98+
}
99+
return sInstance;
93100
}
94101

95102
QgsNetworkAccessManager::QgsNetworkAccessManager( QObject *parent )

src/core/qgsnetworkaccessmanager.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class CORE_EXPORT QgsNetworkAccessManager : public QNetworkAccessManager
4848
public:
4949
//! returns a pointer to the single instance
5050
// and creates that instance on the first call.
51-
static QgsNetworkAccessManager *instance();
51+
static QgsNetworkAccessManager* instance();
5252

5353
QgsNetworkAccessManager( QObject *parent = 0 );
5454

@@ -100,6 +100,7 @@ class CORE_EXPORT QgsNetworkAccessManager : public QNetworkAccessManager
100100
QNetworkProxy mFallbackProxy;
101101
QStringList mExcludedURLs;
102102
bool mUseSystemProxy;
103+
static QgsNetworkAccessManager* sInstance;
103104
};
104105

105106
#endif // QGSNETWORKACCESSMANAGER_H

tests/src/core/testqgsnetworkcontentfetcher.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
***************************************************************************/
1717

1818
#include "qgsnetworkcontentfetcher.h"
19+
#include "qgsapplication.h"
1920
#include <QObject>
2021
#include <QtTest>
2122
#include <QNetworkReply>
@@ -44,22 +45,21 @@ class TestQgsNetworkContentFetcher: public QObject
4445

4546
void TestQgsNetworkContentFetcher::initTestCase()
4647
{
47-
48+
QgsApplication::init();
49+
QgsApplication::initQgis();
4850
}
4951

5052
void TestQgsNetworkContentFetcher::cleanupTestCase()
5153
{
52-
54+
QgsApplication::exitQgis();
5355
}
5456

5557
void TestQgsNetworkContentFetcher::init()
5658
{
57-
5859
}
5960

6061
void TestQgsNetworkContentFetcher::cleanup()
6162
{
62-
6363
}
6464

6565
void TestQgsNetworkContentFetcher::fetchEmptyUrl()

0 commit comments

Comments
 (0)