diff --git a/src/gui/image/qpixmapcache.cpp b/src/gui/image/qpixmapcache.cpp index e6b1f5ea385..46398582568 100644 --- a/src/gui/image/qpixmapcache.cpp +++ b/src/gui/image/qpixmapcache.cpp @@ -259,25 +259,12 @@ bool QPMCache::flushDetachedPixmaps(bool nt) { auto mc = maxCost(); const qsizetype currentTotal = totalCost(); + const qsizetype oldSize = size(); if (currentTotal) setMaxCost(nt ? currentTotal * 3 / 4 : currentTotal - 1); setMaxCost(mc); ps = totalCost(); - - bool any = false; - QHash::iterator it = cacheKeys.begin(); - while (it != cacheKeys.end()) { - const auto value = it.value(); - if (value.isValid() && !contains(value)) { - releaseKey(value); - it = cacheKeys.erase(it); - any = true; - } else { - ++it; - } - } - - return any; + return size() != oldSize; } void QPMCache::timerEvent(QTimerEvent *) @@ -296,17 +283,9 @@ void QPMCache::timerEvent(QTimerEvent *) QPixmap *QPMCache::object(const QString &key) const { - QPixmapCache::Key cacheKey = cacheKeys.value(key); - if (!cacheKey.d || !cacheKey.d->isValid) { - const_cast(this)->cacheKeys.remove(key); - return nullptr; - } - QPixmap *ptr = QCache::object(cacheKey); - //We didn't find the pixmap in the cache, the key is not valid anymore - if (!ptr) { - const_cast(this)->cacheKeys.remove(key); - } - return ptr; + if (const auto it = cacheKeys.find(key); it != cacheKeys.cend()) + return object(it.value()); + return nullptr; } QPixmap *QPMCache::object(const QPixmapCache::Key &key) const @@ -327,6 +306,7 @@ bool QPMCache::insert(const QString& key, const QPixmap &pixmap, int cost) // this will create a new key; the old one has been removed auto k = insert(pixmap, cost); if (k.isValid()) { + k.d->stringKey = key; cacheKeys[key] = std::move(k); return true; } @@ -383,7 +363,11 @@ QPixmapCache::Key QPMCache::createKey() void QPMCache::releaseKey(const QPixmapCache::Key &key) { QPixmapCache::KeyData *keyData = key.d; - if (!keyData || keyData->key > keyArraySize || keyData->key <= 0) + if (!keyData) + return; + if (!keyData->stringKey.isNull()) + cacheKeys.remove(keyData->stringKey); + if (keyData->key > keyArraySize || keyData->key <= 0) return; keyData->key--; keyArray[keyData->key] = freeKey; @@ -410,7 +394,6 @@ void QPMCache::clear() killTimer(theid); theid = 0; } - cacheKeys.clear(); } QPixmapCache::KeyData* QPMCache::getKeyData(QPixmapCache::Key *key) diff --git a/src/gui/image/qpixmapcache_p.h b/src/gui/image/qpixmapcache_p.h index 38a28281b43..43c4d9784c6 100644 --- a/src/gui/image/qpixmapcache_p.h +++ b/src/gui/image/qpixmapcache_p.h @@ -31,6 +31,7 @@ public: : isValid(other.isValid), key(other.key), ref(1) {} ~KeyData() {} + QString stringKey; bool isValid; int key; int ref; diff --git a/tests/auto/gui/image/qpixmapcache/tst_qpixmapcache.cpp b/tests/auto/gui/image/qpixmapcache/tst_qpixmapcache.cpp index 99eeadb994c..e42cdbb7f1f 100644 --- a/tests/auto/gui/image/qpixmapcache/tst_qpixmapcache.cpp +++ b/tests/auto/gui/image/qpixmapcache/tst_qpixmapcache.cpp @@ -542,7 +542,6 @@ void tst_QPixmapCache::evictionDoesNotLeakStringKeys() pm.fill(Qt::transparent); [[maybe_unused]] auto r = QPixmapCache::insert(pm); } - QEXPECT_FAIL("", "QTBUG-112200", Continue); }); } @@ -550,7 +549,6 @@ void tst_QPixmapCache::reducingCacheLimitDoesNotLeakStringKeys() { stringLeak_impl([] { QPixmapCache::setCacheLimit(0); - QEXPECT_FAIL("", "QTBUG-112200", Continue); }); }