Lifetime management of value cache singletons

Hook in to the `destroyed` signal of the `qApp` to destroy the caches.
This ensures any associated workers are quit before application
terminates and kills all the threads.
This commit is contained in:
Mat Sutcliffe
2020-03-28 22:08:11 +00:00
parent 9dbe2f4018
commit e5528288b5
2 changed files with 8 additions and 4 deletions

View File

@@ -19,8 +19,10 @@ namespace BlackMisc
CSettingsCache *CSettingsCache::instance()
{
static CSettingsCache cache;
return &cache;
static std::unique_ptr<CSettingsCache> cache(new CSettingsCache);
static auto dummy = (connect(qApp, &QObject::destroyed, cache.get(), [] { cache.reset(); }), nullptr);
Q_UNUSED(dummy) // declared as static to get thread-safe initialization
return cache.get();
}
const QString &CSettingsCache::persistentStore()