From 51024f879d5051c2fcb1b578f52d779df4774f8e Mon Sep 17 00:00:00 2001 From: apio Date: Fri, 3 Feb 2023 22:19:26 +0100 Subject: [PATCH] SharedPtr: Delete ptr on failure in all adopt_shared* functions --- luna/include/luna/SharedPtr.h | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/luna/include/luna/SharedPtr.h b/luna/include/luna/SharedPtr.h index cdf02fb5..5eb9f918 100644 --- a/luna/include/luna/SharedPtr.h +++ b/luna/include/luna/SharedPtr.h @@ -117,12 +117,17 @@ template Result> make_shared(Args... ar return SharedPtr { ptr, ref_count }; } +// NOTE: ptr is deleted if any of the adopt_shared* functions fail to construct a SharedPtr. template Result> adopt_shared(T* ptr) { using RefCount = __detail::RefCount; + auto guard = make_scope_guard([ptr] { delete ptr; }); + RefCount* const ref_count = TRY(make()); + guard.deactivate(); + return SharedPtr { ptr, ref_count }; } @@ -138,13 +143,7 @@ template Result> adopt_shared_from_owned(OwnedPtr&& T* ptr = other.m_ptr; other.m_ptr = nullptr; - // FIXME: Should the pointee magically vanish on failure? Or go back into the OwnedPtr, even though it's been - // moved... - auto guard = make_scope_guard([&] { delete ptr; }); - const SharedPtr shared_ptr = TRY(adopt_shared(ptr)); - guard.deactivate(); - return shared_ptr; }