diff --git a/API/fleece/RefCounted.hh b/API/fleece/RefCounted.hh index 1e8fe664..04543381 100644 --- a/API/fleece/RefCounted.hh +++ b/API/fleece/RefCounted.hh @@ -48,24 +48,11 @@ namespace fleece { friend void release(const RefCounted* FL_NULLABLE) noexcept; friend void assignRef(RefCounted* FL_NULLABLE &dst, RefCounted* FL_NULLABLE src) noexcept; -#if DEBUG - void _retain() const noexcept {_careful_retain();} - void _release() const noexcept {_careful_release();} -#else - ALWAYS_INLINE void _retain() const noexcept { ++_refCount; } + static constexpr int32_t kCarefulInitialRefCount = -6666666; + void _retain() const noexcept; void _release() const noexcept; -#endif - static constexpr int32_t kCarefulInitialRefCount = -6666666; - void _careful_retain() const noexcept; - void _careful_release() const noexcept; - - mutable std::atomic _refCount -#if DEBUG - {kCarefulInitialRefCount}; -#else - {0}; -#endif + mutable std::atomic _refCount {kCarefulInitialRefCount}; }; template concept RefCountedType = std::derived_from; diff --git a/Fleece/Support/RefCounted.cc b/Fleece/Support/RefCounted.cc index f706e221..013787f8 100644 --- a/Fleece/Support/RefCounted.cc +++ b/Fleece/Support/RefCounted.cc @@ -24,14 +24,6 @@ namespace fleece { -#if !DEBUG - __hot void RefCounted::_release() const noexcept { - if (--_refCount <= 0) - delete this; - } -#endif - - __hot void release(const RefCounted *r) noexcept { if (r) r->_release(); } @@ -74,9 +66,7 @@ namespace fleece { // Store a garbage value to detect use-after-free int32_t oldRef = _refCount.exchange(-9999999); if (_usuallyFalse(oldRef != 0)) { -#if DEBUG if (oldRef != kCarefulInitialRefCount) -#endif { // Detect if the destructor is not called from _release, i.e. the object still has // references. This is probably a direct call to delete, which is illegal. @@ -96,7 +86,7 @@ namespace fleece { // the object. - void RefCounted::_careful_retain() const noexcept { + void RefCounted::_retain() const noexcept { auto oldRef = _refCount++; // Special case: the initial retain of a new object that takes it to refCount 1 @@ -113,7 +103,7 @@ namespace fleece { } - void RefCounted::_careful_release() const noexcept { + void RefCounted::_release() const noexcept { auto oldRef = _refCount--; // If the refCount was 0 we have a bug where another thread is destructing