From b80dc504958e116f40ac6e8ab827be57a30aa4b0 Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Fri, 25 Mar 2022 03:47:43 -0700 Subject: Protect intrusive_ptr and ComPtr from moving to itself --- common/comptr.h | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'common/comptr.h') diff --git a/common/comptr.h b/common/comptr.h index ab9d4c53..5984ebd9 100644 --- a/common/comptr.h +++ b/common/comptr.h @@ -4,6 +4,8 @@ #include #include +#include "opthelpers.h" + template class ComPtr { @@ -42,10 +44,13 @@ public: } ComPtr& operator=(ComPtr&& rhs) { - if(mPtr) - mPtr->Release(); - mPtr = rhs.mPtr; - rhs.mPtr = nullptr; + if(likely(&rhs != this)) + { + if(mPtr) + mPtr->Release(); + mPtr = rhs.mPtr; + rhs.mPtr = nullptr; + } return *this; } @@ -56,12 +61,7 @@ public: T* get() const noexcept { return mPtr; } T** getPtr() noexcept { return &mPtr; } - T* release() noexcept - { - T *ret{mPtr}; - mPtr = nullptr; - return ret; - } + T* release() noexcept { return std::exchange(mPtr, nullptr); } void swap(ComPtr &rhs) noexcept { std::swap(mPtr, rhs.mPtr); } void swap(ComPtr&& rhs) noexcept { std::swap(mPtr, rhs.mPtr); } -- cgit v1.2.3