diff --git a/include/util/pool_allocator.hpp b/include/util/pool_allocator.hpp index 9789e26e1..4758c8210 100644 --- a/include/util/pool_allocator.hpp +++ b/include/util/pool_allocator.hpp @@ -14,6 +14,26 @@ namespace osrm::util { #if 1 + +class Cleanup { +public: + static void CleanupAtExit(void* p) { + std::lock_guard lock(mutex_); + allocated_blocks_.push_back(static_cast(p)); + } + + ~Cleanup() { + std::lock_guard lock(mutex_); + for (auto block : allocated_blocks_) { + std::free(block); + } + } + +private: + static std::vector allocated_blocks_; + static std::mutex mutex_; +}; + template class PoolAllocator; @@ -57,12 +77,12 @@ public: ~MemoryManager() { - for (auto block : blocks_) - { - std::free(block); - } + // std::cerr << "~MemoryManager()" << std::endl; + // for (auto block : blocks_) + // { + // std::free(block); + // } } - private: MemoryManager() = default; MemoryManager(const MemoryManager &) = delete; @@ -84,6 +104,8 @@ private: { throw std::bad_alloc(); } + Cleanup::CleanupAtExit(block); + total_allocated_ += block_size; blocks_.push_back(block); current_block_ptr_ = block;