diff --git a/Source/Engine/Core/Delegate.h b/Source/Engine/Core/Delegate.h index 1b7a28c48..aa2ea630a 100644 --- a/Source/Engine/Core/Delegate.h +++ b/Source/Engine/Core/Delegate.h @@ -290,19 +290,13 @@ protected: intptr volatile _size = 0; #else HashSet* _functions = nullptr; - CriticalSection* _locker; - int32* _lockerRefCount; + CriticalSection* _locker = nullptr; #endif typedef void (*StubSignature)(void*, Params...); public: Delegate() { -#if !DELEGATE_USE_ATOMIC - _locker = New(); - _lockerRefCount = New(); - *_lockerRefCount = 1; -#endif } Delegate(const Delegate& other) @@ -329,8 +323,6 @@ public: i->Item.LambdaCtor(); } _locker = other._locker; - _lockerRefCount = other._lockerRefCount; - *_lockerRefCount = *_lockerRefCount + 1; #endif } @@ -344,10 +336,8 @@ public: #else _functions = other._functions; _locker = other._locker; - _lockerRefCount = other._lockerRefCount; other._functions = nullptr; other._locker = nullptr; - other._lockerRefCount = nullptr; #endif } @@ -366,14 +356,10 @@ public: Allocator::Free((void*)_ptr); } #else - int32& lockerRefCount = *_lockerRefCount; - lockerRefCount--; - if (lockerRefCount == 0) + if (_locker != nullptr) { Allocator::Free(_locker); - Allocator::Free(_lockerRefCount); _locker = nullptr; - _lockerRefCount = nullptr; } if (_functions != nullptr) { @@ -383,6 +369,7 @@ public: i->Item.LambdaCtor(); } Allocator::Free(_functions); + _functions = nullptr; } #endif } @@ -417,10 +404,8 @@ public: #else _functions = other._functions; _locker = other._locker; - _lockerRefCount = other._lockerRefCount; other._functions = nullptr; other._locker = nullptr; - other._lockerRefCount = nullptr; #endif } return *this; @@ -522,6 +507,8 @@ public: Allocator::Free(bindings); } #else + if (_locker == nullptr) + _locker = New(); ScopeLock lock(*_locker); if (_functions == nullptr) _functions = New>(32); @@ -581,6 +568,8 @@ public: } } #else + if (_locker == nullptr) + _locker = New(); ScopeLock lock(*_locker); if (_functions && _functions->Contains(f)) return; @@ -594,9 +583,18 @@ public: template void Unbind() { +#if DELEGATE_USE_ATOMIC FunctionType f; f.template Bind(); Unbind(f); +#else + if (_functions == nullptr) + return; + FunctionType f; + f.template Bind(); + ScopeLock lock(*_locker); + _functions->Remove(f); +#endif } /// @@ -606,9 +604,18 @@ public: template void Unbind(T* callee) { +#if DELEGATE_USE_ATOMIC FunctionType f; f.template Bind(callee); Unbind(f); +#else + if (_functions == nullptr) + return; + FunctionType f; + f.template Bind(callee); + ScopeLock lock(*_locker); + _functions->Remove(f); +#endif } /// @@ -617,8 +624,16 @@ public: /// The method. void Unbind(Signature method) { +#if DELEGATE_USE_ATOMIC FunctionType f(method); Unbind(f); +#else + if (_functions == nullptr) + return; + FunctionType f(method); + ScopeLock lock(*_locker); + _functions->Remove(f); +#endif } ///