path: root/src/network/bearer/qsharednetworksession.cpp
diff options
authorMarc Mutz <>2019-06-05 16:24:00 +0200
committerMarc Mutz <>2019-07-19 16:36:26 +0200
commitcab920d52cde7053caab05fd835855c1e3d8127b (patch)
tree3ef64c2d23dbe0c33d404cfec5982e4a7eae01a2 /src/network/bearer/qsharednetworksession.cpp
parent34e457e873f2b699cf50ce613348e355332b9133 (diff)
QSharedNetworkSessionManager: prune expired QNetworkSessions
Equality for QNetworkConfiguration is defined by identity of the Private object, so even if there are QNCs with the same properties, they will not compare equal. This probably happens when a user roams through different WLANs (e.g. from work via public transport to home and then back), and each time the home WLAN pops up, it will beget a new QNetworkConfiguration, not comparing equal to the previous one. So, over time, we might collect a sizeable amount of QNetworkConfiguration objects just sitting in the cache without ever being able to actually use an associated network session, because they all have long since expired. To fix, prune expired network sessions, and thus their associated QNetworkConfigurations, from the cache. To not run every time, prune only when this size of the cache is larger than 16 (arbitrary number). Change-Id: I11a636f45ccf67420f84b1c79a4453a144de7c5c Reviewed-by: MÃ¥rten Nordheim <>
Diffstat (limited to 'src/network/bearer/qsharednetworksession.cpp')
1 files changed, 14 insertions, 0 deletions
diff --git a/src/network/bearer/qsharednetworksession.cpp b/src/network/bearer/qsharednetworksession.cpp
index af543f77e3..b3e9892f4b 100644
--- a/src/network/bearer/qsharednetworksession.cpp
+++ b/src/network/bearer/qsharednetworksession.cpp
@@ -64,9 +64,23 @@ struct DeleteLater {
+template <typename Container>
+static void maybe_prune_expired(Container &c)
+ if (c.size() > 16) {
+ for (auto it = c.cbegin(), end = c.cend(); it != end; /*erasing*/) {
+ if (!it->second.lock())
+ it = c.erase(it);
+ else
+ ++it;
+ }
+ }
QSharedPointer<QNetworkSession> QSharedNetworkSessionManager::getSession(const QNetworkConfiguration &config)
QSharedNetworkSessionManager *m = sharedNetworkSessionManager();
+ maybe_prune_expired(m->sessions);
auto &entry = m->sessions[config];
//if already have a session, return it
if (auto p = entry.toStrongRef())