Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions platform/SharedPtr.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,13 @@ class SharedPtr {
// 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;
}
}

Expand Down Expand Up @@ -226,16 +229,16 @@ class SharedPtr {
/**
* @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) {
uint32_t new_value = core_util_atomic_decr_u32(_counter, 1);
if (new_value == 0) {
delete _counter;
_counter = NULL;
delete _ptr;
_ptr = NULL;
}
}
}
Expand Down