From 95c3574ad06a8ac709fb3633ad5658bc908d8493 Mon Sep 17 00:00:00 2001 From: Wolfgang Bangerth Date: Thu, 29 Jul 2021 12:54:01 -0600 Subject: [PATCH] Also fix the swap functions. --- include/deal.II/base/aligned_vector.h | 41 ++++----------------------- 1 file changed, 6 insertions(+), 35 deletions(-) diff --git a/include/deal.II/base/aligned_vector.h b/include/deal.II/base/aligned_vector.h index 52cfb401d0..8f82b92b60 100644 --- a/include/deal.II/base/aligned_vector.h +++ b/include/deal.II/base/aligned_vector.h @@ -1888,42 +1888,13 @@ template inline void AlignedVector::swap(AlignedVector &vec) { - // Swap the data in the 'elements' objects. One problem is that this - // also moves the deleter object, but the deleter object is a lambda function - // that references 'this' (i.e., the 'this' pointer of the *moved-from* - // object). So what we actually do is steal the pointer via - // std::unique_ptr::release() and then install our own deleter object that - // mirrors the one used in reserve() below. - // - // We have to do the same for the other object - T *this_element_pointer = elements.release(); - - elements = decltype(elements)(vec.elements.release(), [this](T *ptr) { - if (ptr != nullptr) - { - Assert(this->used_elements_end != nullptr, ExcInternalError()); - - if (std::is_trivial::value == false) - for (T *p = this->used_elements_end - 1; p >= ptr; --p) - p->~T(); - } - - std::free(ptr); - }); - - vec.elements = decltype(vec.elements)(this_element_pointer, [&vec](T *ptr) { - if (ptr != nullptr) - { - Assert(vec.used_elements_end != nullptr, ExcInternalError()); - - if (std::is_trivial::value == false) - for (T *p = vec.used_elements_end - 1; p >= ptr; --p) - p->~T(); - } - - std::free(ptr); - }); + // Swap the data in the 'elements' objects. Then also make sure that + // their respective deleter objects point to the right place. + std::swap(elements, vec.elements); + elements.get_deleter().reset_owning_object(this); + vec.elements.get_deleter().reset_owning_object(&vec); + // Now also swap the remaining members. std::swap(used_elements_end, vec.used_elements_end); std::swap(allocated_elements_end, vec.allocated_elements_end); } -- 2.39.5