From ae949b9d21470a9b9bf200774c246a0b86a69ff6 Mon Sep 17 00:00:00 2001 From: Alexis Menard Date: Mon, 27 Apr 2009 16:31:58 +0200 Subject: QPixmapCache:Remove the old pixmap if you insert one with the same key. If you insert a new pixmap in the cache with a key that was already in the cache then we remove the old pixmap and add the new one. This avoid to fill the memory with garbage even if the cache has a protection to avoid running out of memory. This was discovered with QraphicsView and its cache. We don't need to keep old cached pixmaps for an item. Task-number: KDE Reviewed-by: Trond --- src/gui/image/qpixmapcache.cpp | 5 +++++ tests/auto/qpixmapcache/tst_qpixmapcache.cpp | 10 ++++++++++ 2 files changed, 15 insertions(+) diff --git a/src/gui/image/qpixmapcache.cpp b/src/gui/image/qpixmapcache.cpp index eedb6a3..458d6b9 100644 --- a/src/gui/image/qpixmapcache.cpp +++ b/src/gui/image/qpixmapcache.cpp @@ -187,6 +187,11 @@ bool QPMCache::insert(const QString& key, const QPixmap &pixmap, int cost) cacheKeys.insert(key, cacheKey); return true; } + qint64 oldCacheKey = cacheKeys.value(key, -1); + //If for the same key we add already a pixmap we should delete it + if (oldCacheKey != -1) + QCache::remove(oldCacheKey); + bool success = QCache::insert(cacheKey, new QDetachedPixmap(pixmap), cost); if (success) { cacheKeys.insert(key, cacheKey); diff --git a/tests/auto/qpixmapcache/tst_qpixmapcache.cpp b/tests/auto/qpixmapcache/tst_qpixmapcache.cpp index c163b52..1f515ff 100644 --- a/tests/auto/qpixmapcache/tst_qpixmapcache.cpp +++ b/tests/auto/qpixmapcache/tst_qpixmapcache.cpp @@ -166,6 +166,16 @@ void tst_QPixmapCache::insert() QVERIFY(estimatedNum - 1 <= num <= estimatedNum + 1); QPixmap p3; QPixmapCache::insert("null", p3); + + QPixmap c1(10, 10); + c1.fill(Qt::yellow); + QPixmapCache::insert("custom", c1); + QVERIFY(!c1.isDetached()); + QPixmap c2(10, 10); + c2.fill(Qt::red); + QPixmapCache::insert("custom", c2); + //We have deleted the old pixmap in the cache for the same key + QVERIFY(c1.isDetached()); } void tst_QPixmapCache::remove() -- cgit v0.12