Check thread for icon cache (assert)

This commit is contained in:
Klaus Basan
2017-07-01 17:15:43 +02:00
committed by Mathew Sutcliffe
parent 9ad721b77a
commit 709b9a3dd9
2 changed files with 6 additions and 9 deletions

View File

@@ -12,6 +12,7 @@
#include "blackconfig/buildconfig.h"
#include "blackmisc/fileutils.h"
#include "blackmisc/icons.h"
#include "blackmisc/threadutils.h"
#include <QIcon>
#include <QImage>
@@ -1143,20 +1144,16 @@ namespace BlackMisc
const QPixmap &CIcons::pixmapByResourceFileName(const QString &fileName)
{
//! \fixme KB 20170701 noticed the "cache" is not threadsafe. However, there has never be an issue so far. Added thread assert.
Q_ASSERT_X(!fileName.isEmpty(), Q_FUNC_INFO, "missing filename");
Q_ASSERT_X(CThreadUtils::isCurrentThreadApplicationThread(), Q_FUNC_INFO, "not thread safe");
if (!getResourceFileCache().contains(fileName))
{
const QString path = CFileUtils::appendFilePaths(CBuildConfig::getImagesDir(), fileName);
QPixmap pm;
const bool s = pm.load(path);
if (s)
{
getResourceFileCache().insert(fileName, pm);
}
else
{
getResourceFileCache().insert(fileName, CIcons::empty());
}
getResourceFileCache().insert(fileName, s ? pm : CIcons::empty());
}
return getResourceFileCache()[fileName];
}

View File

@@ -703,7 +703,7 @@ namespace BlackMisc
static QPixmap rotate(int rotateDegrees, const QPixmap &original);
private:
//! File cache for the loaded
//! File cache for the loaded files
static QMap<QString, QPixmap> &getResourceFileCache();
};
}