From 5997cbd714c2862e85f08a99f5ab0ca034ad5c54 Mon Sep 17 00:00:00 2001 From: ShiftyX1 Date: Mon, 8 Dec 2025 19:23:57 +0300 Subject: [PATCH] fixed macOS segfault correct way --- src/util/ObjectsPool.hpp | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/src/util/ObjectsPool.hpp b/src/util/ObjectsPool.hpp index 075024f8..0e330cfb 100644 --- a/src/util/ObjectsPool.hpp +++ b/src/util/ObjectsPool.hpp @@ -7,6 +7,8 @@ #if defined(_WIN32) #include +#else +#include #endif namespace util { @@ -51,20 +53,23 @@ namespace util { std::queue freeObjects; std::mutex mutex; - bool allocateNew() { - std::unique_ptr ptr( + void allocateNew() { + constexpr size_t alignment = alignof(T) < sizeof(void*) ? sizeof(void*) : alignof(T); + constexpr size_t size = sizeof(T); + void* rawPtr = nullptr; #if defined(_WIN32) - _aligned_malloc(sizeof(T), alignof(T)) + rawPtr = _aligned_malloc(size, alignment); #else - std::aligned_alloc(alignof(T), sizeof(T)) -#endif - ); - if (ptr == nullptr) { - return false; + if (posix_memalign(&rawPtr, alignment, size) != 0) { + rawPtr = nullptr; } +#endif + if (rawPtr == nullptr) { + throw std::bad_alloc(); + } + std::unique_ptr ptr(rawPtr); freeObjects.push(ptr.get()); objects.push_back(std::move(ptr)); - return true; } }; }