diff options
author | Chris Robinson <[email protected]> | 2022-03-25 03:47:43 -0700 |
---|---|---|
committer | Chris Robinson <[email protected]> | 2022-03-25 03:47:43 -0700 |
commit | b80dc504958e116f40ac6e8ab827be57a30aa4b0 (patch) | |
tree | ca73c49b0df01352163d104d0ab2d50c0fc63c0d /common/comptr.h | |
parent | 1ee34556a63217ecb49f4986ff6b759377535dc4 (diff) |
Protect intrusive_ptr and ComPtr from moving to itself
Diffstat (limited to 'common/comptr.h')
-rw-r--r-- | common/comptr.h | 20 |
1 files changed, 10 insertions, 10 deletions
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 <cstddef> #include <utility> +#include "opthelpers.h" + template<typename T> 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); } |