fixed possible memory leaks in the constructor and assignment operator of SharedPtr reported by issue #287

This commit is contained in:
Kontinuation 2014-05-19 22:24:51 +08:00
parent efc1585ae3
commit 6e92bdc01e

View File

@ -119,9 +119,14 @@ public:
{ {
} }
SharedPtr(C* ptr): _pCounter(new RC), _ptr(ptr) SharedPtr(C* ptr)
try :
_pCounter(new RC), _ptr(ptr)
{ {
} }
catch (...) {
delete ptr;
}
template <class Other, class OtherRP> template <class Other, class OtherRP>
SharedPtr(const SharedPtr<Other, RC, OtherRP>& ptr): _pCounter(ptr._pCounter), _ptr(const_cast<Other*>(ptr.get())) SharedPtr(const SharedPtr<Other, RC, OtherRP>& ptr): _pCounter(ptr._pCounter), _ptr(const_cast<Other*>(ptr.get()))
@ -143,10 +148,8 @@ public:
{ {
if (get() != ptr) if (get() != ptr)
{ {
RC* pTmp = new RC; SharedPtr tmp(ptr);
release(); swap(tmp);
_pCounter = pTmp;
_ptr = ptr;
} }
return *this; return *this;
} }