Commit ae949b9d21470a9b9bf200774c246a0b86a69ff6

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 / 0)
  
187187 cacheKeys.insert(key, cacheKey);
188188 return true;
189189 }
190 qint64 oldCacheKey = cacheKeys.value(key, -1);
191 //If for the same key we add already a pixmap we should delete it
192 if (oldCacheKey != -1)
193 QCache<qint64, QDetachedPixmap>::remove(oldCacheKey);
194
190195 bool success = QCache<qint64, QDetachedPixmap>::insert(cacheKey, new QDetachedPixmap(pixmap), cost);
191196 if (success) {
192197 cacheKeys.insert(key, cacheKey);
tests/auto/qpixmapcache/tst_qpixmapcache.cpp
(10 / 0)
  
166166 QVERIFY(estimatedNum - 1 <= num <= estimatedNum + 1);
167167 QPixmap p3;
168168 QPixmapCache::insert("null", p3);
169
170 QPixmap c1(10, 10);
171 c1.fill(Qt::yellow);
172 QPixmapCache::insert("custom", c1);
173 QVERIFY(!c1.isDetached());
174 QPixmap c2(10, 10);
175 c2.fill(Qt::red);
176 QPixmapCache::insert("custom", c2);
177 //We have deleted the old pixmap in the cache for the same key
178 QVERIFY(c1.isDetached());
169179}
170180
171181void tst_QPixmapCache::remove()

Comments

Add a new comment:

Login or create an account to post a comment

Add your comment