Merge pull request #9580 from kjbracey-arm/sharedptr_reset

Fix SharedPtr::reset
pull/9602/head
Martin Kojtal 2019-02-04 14:41:34 +01:00 committed by GitHub
commit 6684570dea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 5 additions and 2 deletions

View File

@ -145,10 +145,13 @@ public:
// Clean up by decrementing counter
decrement_counter();
_ptr = ptr;
if (ptr != NULL) {
// Allocate counter on the heap, so it can be shared
_counter = new uint32_t;
*_counter = 1;
} else {
_counter = NULL;
}
}
@ -223,15 +226,15 @@ private:
/**
* @brief Decrement reference counter.
* @details If count reaches zero, free counter and delete object pointed to.
* Does not modify our own pointers - assumption is they will be overwritten
* or destroyed immediately afterwards.
*/
void decrement_counter()
{
if (_ptr != NULL) {
if (core_util_atomic_decr_u32(_counter, 1) == 0) {
delete _counter;
_counter = NULL;
delete _ptr;
_ptr = NULL;
}
}
}