Skip to content

Commit

Permalink
Fix Coverity INVALIDATE_ITERATOR defects.
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacault committed Jan 20, 2025
1 parent ed09541 commit 8e5f8d0
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions source/common/ur_singleton.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,21 +76,25 @@ 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 {
__builtin_unreachable();
}
}

//////////////////////////////////////////////////////////////////////////
/// 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--;
__builtin_unreachable();
}
}

Expand Down

0 comments on commit 8e5f8d0

Please sign in to comment.