@@ -772,8 +772,9 @@ QNetworkConfiguration QNetworkAccessManager::configuration() const
772
772
{
773
773
Q_D (const QNetworkAccessManager);
774
774
775
- if (d->networkSession )
776
- return d->networkSession ->configuration ();
775
+ QSharedPointer<QNetworkSession> session (d->getNetworkSession ());
776
+ if (session)
777
+ return session->configuration ();
777
778
else
778
779
return QNetworkConfiguration ();
779
780
}
@@ -797,11 +798,12 @@ QNetworkConfiguration QNetworkAccessManager::activeConfiguration() const
797
798
{
798
799
Q_D (const QNetworkAccessManager);
799
800
800
- if (d->networkSession ) {
801
+ QSharedPointer<QNetworkSession> networkSession (d->getNetworkSession ());
802
+ if (networkSession) {
801
803
QNetworkConfigurationManager manager;
802
804
803
805
return manager.configurationFromIdentifier (
804
- d-> networkSession ->sessionProperty (QLatin1String (" ActiveConfiguration" )).toString ());
806
+ networkSession->sessionProperty (QLatin1String (" ActiveConfiguration" )).toString ());
805
807
} else {
806
808
return QNetworkConfiguration ();
807
809
}
@@ -836,7 +838,8 @@ QNetworkAccessManager::NetworkAccessibility QNetworkAccessManager::networkAccess
836
838
{
837
839
Q_D (const QNetworkAccessManager);
838
840
839
- if (d->networkSession ) {
841
+ QSharedPointer<QNetworkSession> networkSession (d->getNetworkSession ());
842
+ if (networkSession) {
840
843
// d->online holds online/offline state of this network session.
841
844
if (d->online )
842
845
return d->networkAccessible ;
@@ -848,6 +851,13 @@ QNetworkAccessManager::NetworkAccessibility QNetworkAccessManager::networkAccess
848
851
}
849
852
}
850
853
854
+ QSharedPointer<QNetworkSession> QNetworkAccessManagerPrivate::getNetworkSession () const
855
+ {
856
+ if (networkSessionStrongRef)
857
+ return networkSessionStrongRef;
858
+ return networkSessionWeakRef.toStrongRef ();
859
+ }
860
+
851
861
#endif // QT_NO_BEARERMANAGEMENT
852
862
853
863
/* !
@@ -937,7 +947,7 @@ QNetworkReply *QNetworkAccessManager::createRequest(QNetworkAccessManager::Opera
937
947
return new QDisabledNetworkReply (this , req, op);
938
948
}
939
949
940
- if (!d->networkSession && (d->initializeSession || !d->networkConfiguration .isEmpty ())) {
950
+ if (!d->networkSessionStrongRef && (d->initializeSession || !d->networkConfiguration .isEmpty ())) {
941
951
QNetworkConfigurationManager manager;
942
952
if (!d->networkConfiguration .isEmpty ()) {
943
953
d->createSession (manager.configurationFromIdentifier (d->networkConfiguration ));
@@ -1015,8 +1025,8 @@ void QNetworkAccessManagerPrivate::_q_replyFinished()
1015
1025
// It will not be destroyed immediately, but rather when the connection cache is flushed
1016
1026
// after 2 minutes.
1017
1027
activeReplyCount--;
1018
- if (networkSession && activeReplyCount == 0 )
1019
- networkSession .clear ();
1028
+ if (networkSessionStrongRef && activeReplyCount == 0 )
1029
+ networkSessionStrongRef .clear ();
1020
1030
#endif
1021
1031
}
1022
1032
@@ -1176,25 +1186,29 @@ void QNetworkAccessManagerPrivate::createSession(const QNetworkConfiguration &co
1176
1186
1177
1187
initializeSession = false ;
1178
1188
1189
+ // resurrect weak ref if possible
1190
+ networkSessionStrongRef = networkSessionWeakRef.toStrongRef ();
1191
+
1179
1192
QSharedPointer<QNetworkSession> newSession;
1180
1193
if (config.isValid ())
1181
1194
newSession = QSharedNetworkSessionManager::getSession (config);
1182
1195
1183
- if (networkSession ) {
1196
+ if (networkSessionStrongRef ) {
1184
1197
// do nothing if new and old session are the same
1185
- if (networkSession == newSession)
1198
+ if (networkSessionStrongRef == newSession)
1186
1199
return ;
1187
1200
// disconnect from old session
1188
- QObject::disconnect (networkSession .data (), SIGNAL (opened ()), q, SIGNAL (networkSessionConnected ()));
1189
- QObject::disconnect (networkSession .data (), SIGNAL (closed ()), q, SLOT (_q_networkSessionClosed ()));
1190
- QObject::disconnect (networkSession .data (), SIGNAL (stateChanged (QNetworkSession::State)),
1201
+ QObject::disconnect (networkSessionStrongRef .data (), SIGNAL (opened ()), q, SIGNAL (networkSessionConnected ()));
1202
+ QObject::disconnect (networkSessionStrongRef .data (), SIGNAL (closed ()), q, SLOT (_q_networkSessionClosed ()));
1203
+ QObject::disconnect (networkSessionStrongRef .data (), SIGNAL (stateChanged (QNetworkSession::State)),
1191
1204
q, SLOT (_q_networkSessionStateChanged (QNetworkSession::State)));
1192
1205
}
1193
1206
1194
1207
// switch to new session (null if config was invalid)
1195
- networkSession = newSession;
1208
+ networkSessionStrongRef = newSession;
1209
+ networkSessionWeakRef = networkSessionStrongRef.toWeakRef ();
1196
1210
1197
- if (!networkSession ) {
1211
+ if (!networkSessionStrongRef ) {
1198
1212
online = false ;
1199
1213
1200
1214
if (networkAccessible == QNetworkAccessManager::NotAccessible)
@@ -1206,18 +1220,19 @@ void QNetworkAccessManagerPrivate::createSession(const QNetworkConfiguration &co
1206
1220
}
1207
1221
1208
1222
// connect to new session
1209
- QObject::connect (networkSession .data (), SIGNAL (opened ()), q, SIGNAL (networkSessionConnected ()), Qt::QueuedConnection);
1223
+ QObject::connect (networkSessionStrongRef .data (), SIGNAL (opened ()), q, SIGNAL (networkSessionConnected ()), Qt::QueuedConnection);
1210
1224
// QueuedConnection is used to avoid deleting the networkSession inside its closed signal
1211
- QObject::connect (networkSession .data (), SIGNAL (closed ()), q, SLOT (_q_networkSessionClosed ()), Qt::QueuedConnection);
1212
- QObject::connect (networkSession .data (), SIGNAL (stateChanged (QNetworkSession::State)),
1225
+ QObject::connect (networkSessionStrongRef .data (), SIGNAL (closed ()), q, SLOT (_q_networkSessionClosed ()), Qt::QueuedConnection);
1226
+ QObject::connect (networkSessionStrongRef .data (), SIGNAL (stateChanged (QNetworkSession::State)),
1213
1227
q, SLOT (_q_networkSessionStateChanged (QNetworkSession::State)), Qt::QueuedConnection);
1214
1228
1215
- _q_networkSessionStateChanged (networkSession ->state ());
1229
+ _q_networkSessionStateChanged (networkSessionStrongRef ->state ());
1216
1230
}
1217
1231
1218
1232
void QNetworkAccessManagerPrivate::_q_networkSessionClosed ()
1219
1233
{
1220
1234
Q_Q (QNetworkAccessManager);
1235
+ QSharedPointer<QNetworkSession> networkSession (getNetworkSession ());
1221
1236
if (networkSession) {
1222
1237
networkConfiguration = networkSession->configuration ().identifier ();
1223
1238
@@ -1226,7 +1241,8 @@ void QNetworkAccessManagerPrivate::_q_networkSessionClosed()
1226
1241
QObject::disconnect (networkSession.data (), SIGNAL (closed ()), q, SLOT (_q_networkSessionClosed ()));
1227
1242
QObject::disconnect (networkSession.data (), SIGNAL (stateChanged (QNetworkSession::State)),
1228
1243
q, SLOT (_q_networkSessionStateChanged (QNetworkSession::State)));
1229
- networkSession.clear ();
1244
+ networkSessionStrongRef.clear ();
1245
+ networkSessionWeakRef.clear ();
1230
1246
}
1231
1247
}
1232
1248
0 commit comments