SharedPtr: Delete ptr on failure in all adopt_shared* functions
This commit is contained in:
parent
38f0d3a4e6
commit
be408c4cdb
@ -123,10 +123,18 @@ template <typename T> Result<SharedPtr<T>> adopt_shared(T* ptr)
|
|||||||
return SharedPtr<T> { ptr, ref_count };
|
return SharedPtr<T> { ptr, ref_count };
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T, class... Args> Result<SharedPtr<T>> make_shared(Args... args)
|
// 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)
|
||||||
{
|
{
|
||||||
T* raw_ptr = TRY(make<T>(args...));
|
using RefCount = __detail::RefCount;
|
||||||
return adopt_shared(raw_ptr);
|
|
||||||
|
auto guard = make_scope_guard([ptr] { delete ptr; });
|
||||||
|
|
||||||
|
RefCount* const ref_count = TRY(make<RefCount>());
|
||||||
|
|
||||||
|
guard.deactivate();
|
||||||
|
|
||||||
|
return SharedPtr<T> { ptr, ref_count };
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T> Result<SharedPtr<T>> adopt_shared_if_nonnull(T* ptr)
|
template <typename T> Result<SharedPtr<T>> adopt_shared_if_nonnull(T* ptr)
|
||||||
|
Loading…
Reference in New Issue
Block a user