]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Fix a memory leak in AlignedVector.
authorWolfgang Bangerth <bangerth@colostate.edu>
Mon, 29 Nov 2021 22:26:28 +0000 (15:26 -0700)
committerWolfgang Bangerth <bangerth@colostate.edu>
Mon, 29 Nov 2021 22:26:28 +0000 (15:26 -0700)
include/deal.II/base/aligned_vector.h

index e6ed66e0106badea65f04dab42825082a451e6d6..a977a541688e1d093a645462a2e44c8dd4dca3e0 100644 (file)
@@ -848,7 +848,7 @@ namespace internal
 
 
   /**
-   * A class that given a range of memory locations calls either calls
+   * A class that given a range of memory locations either calls
    * the placement-new operator on these memory locations (if
    * `initialize_memory==true`) or just copies the given initializer
    * into this memory location (if `initialize_memory==false`). The
@@ -1199,11 +1199,22 @@ template <class T>
 inline AlignedVector<T> &
 AlignedVector<T>::operator=(const AlignedVector<T> &vec)
 {
+  const size_type new_size = vec.used_elements_end - vec.elements.get();
+
+  // First throw away everything and re-allocate memory but leave that
+  // memory uninitialized for now:
   resize(0);
-  resize_fast(vec.used_elements_end - vec.elements.get());
+  reserve(new_size);
+
+  // Then copy the elements over by using the copy constructor on these
+  // elements:
   internal::AlignedVectorCopyConstruct<T>(vec.elements.get(),
                                           vec.used_elements_end,
                                           elements.get());
+
+  // Finally adjust the pointer to the end of the elements that are used:
+  used_elements_end = elements.get() + new_size;
+
   return *this;
 }
 

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.