Skip to content

Commit

Permalink
AutoPtr: do 'duplicate' before 'release'
Browse files Browse the repository at this point in the history
Common knowledge in reference counting is "on assignment increment first
then decrement", because "just to be deleted" object could hold last
reference to "just to be assigned" one.

Fixes #3979
  • Loading branch information
funny-falcon committed Jun 30, 2023
1 parent ead93ba commit cf846b8
Showing 1 changed file with 6 additions and 17 deletions.
23 changes: 6 additions & 17 deletions Foundation/include/Poco/AutoPtr.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,34 +110,23 @@ class AutoPtr
{
if (_ptr != ptr)
{
if (_ptr) _ptr->release();
_ptr = ptr;
if (shared && _ptr) _ptr->duplicate();
if (shared && ptr) ptr->duplicate();
std::swap(_ptr, ptr);
if (ptr) ptr->release();
}
return *this;
}

AutoPtr& assign(const AutoPtr& ptr)
{
if (&ptr != this)
{
if (_ptr) _ptr->release();
_ptr = ptr._ptr;
if (_ptr) _ptr->duplicate();
}
return *this;
return assign(ptr._ptr, true);
}

template <class Other>
AutoPtr& assign(const AutoPtr<Other>& ptr)
{
if (ptr.get() != _ptr)
{
if (_ptr) _ptr->release();
_ptr = const_cast<Other*>(ptr.get());
if (_ptr) _ptr->duplicate();
}
return *this;
C* nptr = const_cast<Other*>(ptr.get());
return assign(nptr, true);
}

void reset()
Expand Down

0 comments on commit cf846b8

Please sign in to comment.