summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSamuel Rødal <sroedal@trolltech.com>2009-12-10 16:22:53 (GMT)
committerSamuel Rødal <sroedal@trolltech.com>2009-12-10 17:43:30 (GMT)
commit3a8a1f83d60ec16e4c61e2b0a327a5af02917a5a (patch)
treef73f7623cb2eb3fc76e4c1333364216d884ae4bd
parentbf9456c5a2d8dfe9a35a2175186630cb426858ad (diff)
downloadQt-3a8a1f83d60ec16e4c61e2b0a327a5af02917a5a.zip
Qt-3a8a1f83d60ec16e4c61e2b0a327a5af02917a5a.tar.gz
Qt-3a8a1f83d60ec16e4c61e2b0a327a5af02917a5a.tar.bz2
Prevented leak of keys in QPixmapCache.
Removing a pixmap from the pixmap cache using the new QPixmapCache::Key API left the keys dangling in the QCache's internal QHash. The problem is that the Key is invalidated as soon as the QPixmapCacheEntry is destroyed, thus removing it from the hash failed. Reordering the destruction of the object and the removal of the key in QHash fixes the problem. Reviewed-by: Alexis Reviewed-by: Thiago
-rw-r--r--src/corelib/tools/qcache.h2
-rw-r--r--src/gui/image/qpixmapcache.cpp5
-rw-r--r--tests/auto/qpixmapcache/tst_qpixmapcache.cpp19
3 files changed, 25 insertions, 1 deletions
diff --git a/src/corelib/tools/qcache.h b/src/corelib/tools/qcache.h
index 46e20b1..ee9523f 100644
--- a/src/corelib/tools/qcache.h
+++ b/src/corelib/tools/qcache.h
@@ -70,8 +70,8 @@ class QCache
if (l == &n) l = n.p;
if (f == &n) f = n.n;
total -= n.c;
- delete n.t;
hash.remove(*n.keyPtr);
+ delete n.t;
}
inline T *relink(const Key &key) {
typename QHash<Key, Node>::iterator i = hash.find(key);
diff --git a/src/gui/image/qpixmapcache.cpp b/src/gui/image/qpixmapcache.cpp
index b0b7d72..b7b29d7 100644
--- a/src/gui/image/qpixmapcache.cpp
+++ b/src/gui/image/qpixmapcache.cpp
@@ -424,6 +424,11 @@ QPixmapCache::KeyData* QPMCache::getKeyData(QPixmapCache::Key *key)
Q_GLOBAL_STATIC(QPMCache, pm_cache)
+int Q_AUTOTEST_EXPORT q_QPixmapCache_keyHashSize()
+{
+ return pm_cache()->size();
+}
+
QPixmapCacheEntry::~QPixmapCacheEntry()
{
pm_cache()->releaseKey(key);
diff --git a/tests/auto/qpixmapcache/tst_qpixmapcache.cpp b/tests/auto/qpixmapcache/tst_qpixmapcache.cpp
index 9775d36..c89a182 100644
--- a/tests/auto/qpixmapcache/tst_qpixmapcache.cpp
+++ b/tests/auto/qpixmapcache/tst_qpixmapcache.cpp
@@ -70,6 +70,7 @@ private slots:
void remove();
void clear();
void pixmapKey();
+ void noLeak();
};
static QPixmapCache::KeyData* getPrivate(QPixmapCache::Key &key)
@@ -482,5 +483,23 @@ void tst_QPixmapCache::pixmapKey()
QVERIFY(!getPrivate(key8));
}
+extern int q_QPixmapCache_keyHashSize();
+
+void tst_QPixmapCache::noLeak()
+{
+ QPixmapCache::Key key;
+
+ int oldSize = q_QPixmapCache_keyHashSize();
+ for (int i = 0; i < 100; ++i) {
+ QPixmap pm(128, 128);
+ pm.fill(Qt::transparent);
+ key = QPixmapCache::insert(pm);
+ QPixmapCache::remove(key);
+ }
+ int newSize = q_QPixmapCache_keyHashSize();
+
+ QCOMPARE(oldSize, newSize);
+}
+
QTEST_MAIN(tst_QPixmapCache)
#include "tst_qpixmapcache.moc"