Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Coverity INVALIDATE_ITERATOR defects. #2584

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions source/common/ur_singleton.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
#include <mutex>
#include <unordered_map>

#include "logger/ur_logger.hpp"

//////////////////////////////////////////////////////////////////////////
/// a abstract factory for creation of singleton objects
template <typename singleton_tn, typename key_tn> class singleton_factory_t {
Expand Down Expand Up @@ -76,21 +78,27 @@ template <typename singleton_tn, typename key_tn> class singleton_factory_t {

void retain(key_tn key) {
std::lock_guard<std::mutex> 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<std::mutex> 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();
}
}

Expand Down
Loading