From a9c3aa543891fe05ab2debcb2df715645f19c333 Mon Sep 17 00:00:00 2001 From: Stefan Boberg Date: Mon, 24 May 2021 16:07:51 +0200 Subject: RefPtr/Ref cleanup Fixed self-assignment bug in move assignment operator --- zencore/include/zencore/refcount.h | 41 +++++++++++++++++++++++++------------- 1 file changed, 27 insertions(+), 14 deletions(-) (limited to 'zencore/include') diff --git a/zencore/include/zencore/refcount.h b/zencore/include/zencore/refcount.h index e6cdbc540..288b649c6 100644 --- a/zencore/include/zencore/refcount.h +++ b/zencore/include/zencore/refcount.h @@ -74,16 +74,22 @@ public: } inline RefPtr& operator=(const RefPtr& Rhs) { - m_Ref && m_Ref->Release(); - auto Ref = m_Ref = Rhs.m_Ref; - Ref && Ref->AddRef(); + if (&Rhs != this) + { + Rhs && Rhs->AddRef(); + m_Ref && m_Ref->Release(); + m_Ref = Rhs.m_Ref; + } return *this; } inline RefPtr& operator=(RefPtr&& Rhs) noexcept { - m_Ref && m_Ref->Release(); - m_Ref = Rhs.m_Ref; - Rhs.m_Ref = nullptr; + if (&Rhs != this) + { + m_Ref && m_Ref->Release(); + m_Ref = Rhs.m_Ref; + Rhs.m_Ref = nullptr; + } return *this; } inline RefPtr(RefPtr&& Rhs) noexcept : m_Ref(Rhs.m_Ref) { Rhs.m_Ref = nullptr; } @@ -108,8 +114,9 @@ public: inline Ref(T* Ptr) : m_Ref(Ptr) { m_Ref && m_Ref->AddRef(); } inline ~Ref() { m_Ref && m_Ref->Release(); } - inline explicit operator bool() const { return m_Ref != nullptr; } - inline T* operator->() const { return m_Ref; } + [[nodiscard]] inline bool IsNull() const { return m_Ref == nullptr; } + inline explicit operator bool() const { return m_Ref != nullptr; } + inline T* operator->() const { return m_Ref; } inline std::strong_ordering operator<=>(const Ref& Rhs) const = default; @@ -122,16 +129,22 @@ public: } inline Ref& operator=(const Ref& Rhs) { - m_Ref && m_Ref->Release(); - auto Ref = m_Ref = Rhs.m_Ref; - Ref && Ref->AddRef(); + if (&Rhs != this) + { + Rhs && Rhs->AddRef(); + m_Ref && m_Ref->Release(); + m_Ref = Rhs.m_Ref; + } return *this; } inline Ref& operator=(Ref&& Rhs) noexcept { - m_Ref && m_Ref->Release(); - m_Ref = Rhs.m_Ref; - Rhs.m_Ref = nullptr; + if (&Rhs != this) + { + m_Ref && m_Ref->Release(); + m_Ref = Rhs.m_Ref; + Rhs.m_Ref = nullptr; + } return *this; } inline Ref(Ref&& Rhs) noexcept : m_Ref(Rhs.m_Ref) { Rhs.m_Ref = nullptr; } -- cgit v1.2.3