diff --git a/base/src/refbase.cpp b/base/src/refbase.cpp index 1b2968ee190f15c56e8416d409fdcede2d830109..a64f89590d23a435643a1c05625b42ecf077af37 100644 --- a/base/src/refbase.cpp +++ b/base/src/refbase.cpp @@ -79,6 +79,7 @@ void RefCounter::DecRefCount() { if (atomicRefCount_.load(std::memory_order_relaxed) > 0) { if (atomicRefCount_.fetch_sub(1, std::memory_order_release) == 1) { + std::atomic_thread_fence(std::memory_order_acquire); delete (this); } } @@ -152,7 +153,8 @@ int RefCounter::DecWeakRefCount(const void*) return curCount; } - if (IsLifeTimeExtended() && GetStrongRefCount() == 0) { + std::atomic_thread_fence(std::memory_order_acquire); + if (IsLifeTimeExtended()) { if (callback_) { callback_(); } @@ -230,7 +232,7 @@ bool RefCounter::AttemptIncStrongRef(const void *objectId, int &outCount) } ATTEMPT_SUCCESS: - if (curCount >= INITIAL_PRIMARY_VALUE) { + if (curCount == INITIAL_PRIMARY_VALUE) { outCount = curCount; atomicStrong_.fetch_sub(INITIAL_PRIMARY_VALUE, std::memory_order_release); return true; @@ -348,8 +350,8 @@ void RefBase::IncStrongRef(const void *objectId) return; } - const int curCount = refs_->IncStrongRefCount(objectId); IncWeakRef(objectId); + const int curCount = refs_->IncStrongRefCount(objectId); if (curCount == INITIAL_PRIMARY_VALUE) { OnFirstStrongRef(objectId); } @@ -369,6 +371,7 @@ void RefBase::DecStrongRef(const void *objectId) RefCounter * const refs = refs_; const int curCount = refs->DecStrongRefCount(objectId); if (curCount == 1) { + std::atomic_thread_fence(std::memory_order_acquire); OnLastStrongRef(objectId); if (!refs->IsLifeTimeExtended()) { if (refs->callback_) {