From 60d79365495c4743ac6380d9b72755170cf61913 Mon Sep 17 00:00:00 2001 From: Wolfgang Bangerth Date: Mon, 19 Oct 2020 19:07:16 -0600 Subject: [PATCH] Let PropertyPool keep track of the memory it allocates. --- include/deal.II/particles/property_pool.h | 17 +++++++++++++++++ source/particles/property_pool.cc | 18 +++++++++++++++++- 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/include/deal.II/particles/property_pool.h b/include/deal.II/particles/property_pool.h index 4689e191f2..659280b6ef 100644 --- a/include/deal.II/particles/property_pool.h +++ b/include/deal.II/particles/property_pool.h @@ -20,6 +20,9 @@ #include +#include + + DEAL_II_NAMESPACE_OPEN namespace Particles @@ -60,6 +63,13 @@ namespace Particles */ PropertyPool(const unsigned int n_properties_per_slot); + /** + * Destructor. This function ensures that all memory that had + * previously been allocated using allocate_properties_array() + * has also been returned via deallocate_properties_array(). + */ + ~PropertyPool(); + /** * Return a new handle that allows accessing the reserved block * of memory. If the number of properties is zero this will return an @@ -101,6 +111,13 @@ namespace Particles * The number of properties that are reserved per particle. */ const unsigned int n_properties; + + /** + * A collection of handles that have been created by + * allocate_properties_array() and have not been destroyed by + * deallocate_properties_array(). + */ + std::set currently_open_handles; }; diff --git a/source/particles/property_pool.cc b/source/particles/property_pool.cc index 56d3677871..e429b9ff6c 100644 --- a/source/particles/property_pool.cc +++ b/source/particles/property_pool.cc @@ -28,13 +28,27 @@ namespace Particles {} + PropertyPool::~PropertyPool() + { + Assert(currently_open_handles.size() == 0, + ExcMessage("This property pool currently still holds " + + std::to_string(currently_open_handles.size()) + + " open handles to memory that was allocated " + "via allocate_properties_array() but that has " + "not been returned via deallocate_properties_array().")); + } + + PropertyPool::Handle PropertyPool::allocate_properties_array() { PropertyPool::Handle handle = PropertyPool::invalid_handle; if (n_properties > 0) - handle = new double[n_properties]; + { + handle = new double[n_properties]; + currently_open_handles.insert(handle); + } return handle; } @@ -44,6 +58,8 @@ namespace Particles void PropertyPool::deallocate_properties_array(Handle handle) { + Assert(currently_open_handles.count(handle) == 1, ExcInternalError()); + currently_open_handles.erase(handle); delete[] handle; } -- 2.39.5