From f1c5b1a4345c48cb498570dca6a11c4947aa88bc Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Fri, 15 Dec 2023 21:38:20 -0800 Subject: Catch a potential exception to free memory --- common/flexarray.h | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) (limited to 'common/flexarray.h') diff --git a/common/flexarray.h b/common/flexarray.h index c317bfbe..ad15d554 100644 --- a/common/flexarray.h +++ b/common/flexarray.h @@ -2,6 +2,7 @@ #define AL_FLEXARRAY_H #include +#include #include #include "almalloc.h" @@ -69,11 +70,20 @@ struct FlexArray { { return Storage_t_::Sizeof(count, base); } static std::unique_ptr Create(index_type count) { - void *ptr{al_calloc(alignof(FlexArray), Sizeof(count))}; - return std::unique_ptr{al::construct_at(static_cast(ptr), count)}; + if(void *ptr{al_calloc(alignof(FlexArray), Sizeof(count))}) + { + try { + return std::unique_ptr{::new(ptr) FlexArray{count}}; + } + catch(...) { + al_free(ptr); + throw; + } + } + throw std::bad_alloc(); } - FlexArray(index_type size) : mStore{size} { } + FlexArray(index_type size) noexcept(noexcept(Storage_t_{size})) : mStore{size} { } ~FlexArray() = default; [[nodiscard]] auto size() const noexcept -> index_type { return mStore.mData.size(); } -- cgit v1.2.3