Skip to content

Commit

Permalink
fixed unique_ptr::reset() instructions order. Internal pointer must b…
Browse files Browse the repository at this point in the history
…e updated before deleting object (#373)
  • Loading branch information
Wawha authored Jun 9, 2020
1 parent d996510 commit d84b9c6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
6 changes: 2 additions & 4 deletions include/EASTL/unique_ptr.h
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,8 @@ namespace eastl
{
if (pValue != mPair.first())
{
if (mPair.first())
get_deleter()(mPair.first());

mPair.first() = pValue;
if (auto first = exchange(mPair.first(), pValue))
get_deleter()(first);
}
}

Expand Down
24 changes: 24 additions & 0 deletions test/source/TestSmartPtr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,20 @@ namespace SmartPtrTest
int mX;
};

struct CheckUPtrEmptyInDestructor
{
~CheckUPtrEmptyInDestructor()
{
if(mpUPtr)
mCheckUPtrEmpty = (*mpUPtr == nullptr);
}

eastl::unique_ptr<CheckUPtrEmptyInDestructor>* mpUPtr{};
static bool mCheckUPtrEmpty;
};

bool CheckUPtrEmptyInDestructor::mCheckUPtrEmpty = false;

} // namespace SmartPtrTest


Expand Down Expand Up @@ -542,6 +556,16 @@ static int Test_unique_ptr()
}
}

{
// Test that unique_ptr internal pointer is reset before calling the destructor
CheckUPtrEmptyInDestructor::mCheckUPtrEmpty = false;

unique_ptr<CheckUPtrEmptyInDestructor> uptr(new CheckUPtrEmptyInDestructor);
uptr->mpUPtr = &uptr;
uptr.reset();
EATEST_VERIFY(CheckUPtrEmptyInDestructor::mCheckUPtrEmpty);
}

{
#if EASTL_CORE_ALLOCATOR_ENABLED
// Test EA::Allocator::EASTLICoreDeleter usage within eastl::shared_ptr.
Expand Down

0 comments on commit d84b9c6

Please sign in to comment.