diff options
author | Chris Robinson <[email protected]> | 2023-12-15 21:38:20 -0800 |
---|---|---|
committer | Chris Robinson <[email protected]> | 2023-12-15 21:38:20 -0800 |
commit | f1c5b1a4345c48cb498570dca6a11c4947aa88bc (patch) | |
tree | 049eb60c062fdd28fe61db797cf42b6a79ee28d0 /common | |
parent | 41266e96aeb0cac54d05a9e57dcea0990933dd33 (diff) |
Catch a potential exception to free memory
Diffstat (limited to 'common')
-rw-r--r-- | common/flexarray.h | 16 |
1 files changed, 13 insertions, 3 deletions
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 <cstddef> +#include <stdexcept> #include <type_traits> #include "almalloc.h" @@ -69,11 +70,20 @@ struct FlexArray { { return Storage_t_::Sizeof(count, base); } static std::unique_ptr<FlexArray> Create(index_type count) { - void *ptr{al_calloc(alignof(FlexArray), Sizeof(count))}; - return std::unique_ptr<FlexArray>{al::construct_at(static_cast<FlexArray*>(ptr), count)}; + if(void *ptr{al_calloc(alignof(FlexArray), Sizeof(count))}) + { + try { + return std::unique_ptr<FlexArray>{::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(); } |