From cab920d52cde7053caab05fd835855c1e3d8127b Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Wed, 5 Jun 2019 16:24:00 +0200 Subject: QSharedNetworkSessionManager: prune expired QNetworkSessions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- src/network/bearer/qsharednetworksession.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'src') 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 +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 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()) -- cgit v1.2.3