diff --git a/src/util/ObjectsPool.hpp b/src/util/ObjectsPool.hpp index 55238959..075024f8 100644 --- a/src/util/ObjectsPool.hpp +++ b/src/util/ObjectsPool.hpp @@ -33,7 +33,9 @@ namespace util { std::shared_ptr create(Args&&... args) { std::lock_guard lock(mutex); if (freeObjects.empty()) { - allocateNew(); + if (!allocateNew()) { + return std::make_shared(std::forward(args)...); + } } auto ptr = freeObjects.front(); freeObjects.pop(); @@ -49,7 +51,7 @@ namespace util { std::queue freeObjects; std::mutex mutex; - void allocateNew() { + bool allocateNew() { std::unique_ptr ptr( #if defined(_WIN32) _aligned_malloc(sizeof(T), alignof(T)) @@ -57,8 +59,12 @@ namespace util { std::aligned_alloc(alignof(T), sizeof(T)) #endif ); + if (ptr == nullptr) { + return false; + } freeObjects.push(ptr.get()); objects.push_back(std::move(ptr)); + return true; } }; }