From: Wolfgang Bangerth Date: Mon, 29 Nov 2021 22:26:28 +0000 (-0700) Subject: Fix a memory leak in AlignedVector. X-Git-Tag: v9.4.0-rc1~785^2~1 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d59e57ee24116490e20b2036ae5876585341f294;p=dealii.git Fix a memory leak in AlignedVector. --- diff --git a/include/deal.II/base/aligned_vector.h b/include/deal.II/base/aligned_vector.h index e6ed66e010..a977a54168 100644 --- a/include/deal.II/base/aligned_vector.h +++ b/include/deal.II/base/aligned_vector.h @@ -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 inline AlignedVector & AlignedVector::operator=(const AlignedVector &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(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; }