diff --git a/cpp/core/utils/ObjectStore.cc b/cpp/core/utils/ObjectStore.cc index 05fd5843733..4e8df376233 100644 --- a/cpp/core/utils/ObjectStore.cc +++ b/cpp/core/utils/ObjectStore.cc @@ -46,12 +46,13 @@ std::pair gluten::ObjectStore::loo gluten::ObjectStore::~ObjectStore() { for (;;) { - if (aliveObjects_.empty()) { - break; - } std::shared_ptr tempObj; { const std::lock_guard lock(mtx_); + // The empty-check and the pop of the last alive object must be atomic under `mtx_`. + if (aliveObjects_.empty()) { + break; + } // destructing in reversed order (the last added object destructed first) auto itr = aliveObjects_.rbegin(); const ResourceHandle handle = (*itr).first;