SharedPtr: Delete ptr on failure in all adopt_shared* functions
This commit is contained in:
parent
89958fbc74
commit
51024f879d
@ -117,12 +117,17 @@ template <typename T, class... Args> Result<SharedPtr<T>> make_shared(Args... ar
|
|||||||
return SharedPtr<T> { ptr, ref_count };
|
return SharedPtr<T> { ptr, ref_count };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NOTE: ptr is deleted if any of the adopt_shared* functions fail to construct a SharedPtr.
|
||||||
template <typename T> Result<SharedPtr<T>> adopt_shared(T* ptr)
|
template <typename T> Result<SharedPtr<T>> adopt_shared(T* ptr)
|
||||||
{
|
{
|
||||||
using RefCount = __detail::RefCount;
|
using RefCount = __detail::RefCount;
|
||||||
|
|
||||||
|
auto guard = make_scope_guard([ptr] { delete ptr; });
|
||||||
|
|
||||||
RefCount* const ref_count = TRY(make<RefCount>());
|
RefCount* const ref_count = TRY(make<RefCount>());
|
||||||
|
|
||||||
|
guard.deactivate();
|
||||||
|
|
||||||
return SharedPtr<T> { ptr, ref_count };
|
return SharedPtr<T> { ptr, ref_count };
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -138,13 +143,7 @@ template <typename T> Result<SharedPtr<T>> adopt_shared_from_owned(OwnedPtr<T>&&
|
|||||||
T* ptr = other.m_ptr;
|
T* ptr = other.m_ptr;
|
||||||
other.m_ptr = nullptr;
|
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<T> shared_ptr = TRY(adopt_shared(ptr));
|
const SharedPtr<T> shared_ptr = TRY(adopt_shared(ptr));
|
||||||
|
|
||||||
guard.deactivate();
|
|
||||||
|
|
||||||
return shared_ptr;
|
return shared_ptr;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user