From 0b27c9149e5154909be930c405bbef5d8b0fca35 Mon Sep 17 00:00:00 2001 From: Kevin Bracey Date: Thu, 31 Jan 2019 16:35:53 +0200 Subject: [PATCH] Fix SharedPtr::reset SharedPtr::reset did not actually set the stored pointer value. Correct this, with other minor tidies: * Ensure counter is set to NULL if pointer is reset to NULL * Be consistent about not clearing pointers in decrement_counter(). --- platform/SharedPtr.h | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/platform/SharedPtr.h b/platform/SharedPtr.h index 0663b2147d..28111f0936 100644 --- a/platform/SharedPtr.h +++ b/platform/SharedPtr.h @@ -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; } } @@ -226,6 +229,8 @@ 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() { @@ -233,9 +238,7 @@ private: uint32_t new_value = core_util_atomic_decr_u32(_counter, 1); if (new_value == 0) { delete _counter; - _counter = NULL; delete _ptr; - _ptr = NULL; } } }