Skip to content

Commit

Permalink
fixed unique_ptr<[]>::reset() instructions order. Internal pointer mu…
Browse files Browse the repository at this point in the history
…st be updated before deleting object (#375)
  • Loading branch information
Wawha authored Jun 10, 2020
1 parent b2d923a commit 1cf6182
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
6 changes: 3 additions & 3 deletions include/EASTL/unique_ptr.h
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ namespace eastl
{
if (pValue != mPair.first())
{
if (auto first = exchange(mPair.first(), pValue))
if (auto first = eastl::exchange(mPair.first(), pValue))
get_deleter()(first);
}
}
Expand Down Expand Up @@ -431,8 +431,8 @@ namespace eastl
{
if(pArray != mPair.first())
{
get_deleter()(mPair.first());
mPair.first() = pArray;
if (auto first = eastl::exchange(mPair.first(), pArray))
get_deleter()(first);
}
}

Expand Down
23 changes: 23 additions & 0 deletions test/source/TestSmartPtr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,19 @@ namespace SmartPtrTest

bool CheckUPtrEmptyInDestructor::mCheckUPtrEmpty = false;

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

eastl::unique_ptr<CheckUPtrArrayEmptyInDestructor[]>* mpUPtr{};
static bool mCheckUPtrEmpty;
};

bool CheckUPtrArrayEmptyInDestructor::mCheckUPtrEmpty = false;
} // namespace SmartPtrTest


Expand Down Expand Up @@ -566,6 +579,16 @@ static int Test_unique_ptr()
EATEST_VERIFY(CheckUPtrEmptyInDestructor::mCheckUPtrEmpty);
}

{
// Test that unique_ptr<[]> internal pointer is reset before calling the destructor
CheckUPtrArrayEmptyInDestructor::mCheckUPtrEmpty = false;

unique_ptr<CheckUPtrArrayEmptyInDestructor[]> uptr(new CheckUPtrArrayEmptyInDestructor[1]);
uptr[0].mpUPtr = &uptr;
uptr.reset();
EATEST_VERIFY(CheckUPtrArrayEmptyInDestructor::mCheckUPtrEmpty);
}

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

0 comments on commit 1cf6182

Please sign in to comment.