diff --git a/source/common/ur_singleton.hpp b/source/common/ur_singleton.hpp index b95c12c5d6..30471d0a53 100644 --- a/source/common/ur_singleton.hpp +++ b/source/common/ur_singleton.hpp @@ -17,6 +17,8 @@ #include #include +#include "logger/ur_logger.hpp" + ////////////////////////////////////////////////////////////////////////// /// a abstract factory for creation of singleton objects template class singleton_factory_t { @@ -76,21 +78,27 @@ template class singleton_factory_t { void retain(key_tn key) { std::lock_guard lk(mut); - auto iter = map.find(getKey(key)); - assert(iter != map.end()); - iter->second.ref_count++; + if (auto iter = map.find(getKey(key)); iter != map.end()) { + iter->second.ref_count++; + } else { + logger::error("Invalid singleton key. Aborting."); + std::abort(); + } } ////////////////////////////////////////////////////////////////////////// /// once the key is no longer valid, release the singleton void release(key_tn key) { std::lock_guard lk(mut); - auto iter = map.find(getKey(key)); - assert(iter != map.end()); - if (iter->second.ref_count == 0) { - map.erase(iter); + if (auto iter = map.find(getKey(key)); iter != map.end()) { + if (iter->second.ref_count == 0) { + map.erase(iter); + } else { + iter->second.ref_count--; + } } else { - iter->second.ref_count--; + logger::error("Invalid singleton key. Aborting."); + std::abort(); } }