]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Let PropertyPool keep track of the memory it allocates.
authorWolfgang Bangerth <bangerth@colostate.edu>
Tue, 20 Oct 2020 01:07:16 +0000 (19:07 -0600)
committerWolfgang Bangerth <bangerth@colostate.edu>
Tue, 20 Oct 2020 02:46:50 +0000 (20:46 -0600)
include/deal.II/particles/property_pool.h
source/particles/property_pool.cc

index 4689e191f2061b439348a3bc3f574f9c5fa3bc8c..659280b6efeaf13b25347e370625a2ed53b501e4 100644 (file)
@@ -20,6 +20,9 @@
 
 #include <deal.II/base/array_view.h>
 
+#include <set>
+
+
 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<Handle> currently_open_handles;
   };
 
 
index 56d36778717ccbdb158707c86867da1b817f8e9d..e429b9ff6c7445f6ac027d9cf6d1d683a3056634 100644 (file)
@@ -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;
   }
 

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.