From: Wolfgang Bangerth Date: Tue, 20 Oct 2020 01:07:16 +0000 (-0600) Subject: Let PropertyPool keep track of the memory it allocates. X-Git-Tag: v9.3.0-rc1~997^2~3 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=60d79365495c4743ac6380d9b72755170cf61913;p=dealii.git Let PropertyPool keep track of the memory it allocates. --- 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; }