]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Also fix the swap functions.
authorWolfgang Bangerth <bangerth@colostate.edu>
Thu, 29 Jul 2021 18:54:01 +0000 (12:54 -0600)
committerWolfgang Bangerth <bangerth@colostate.edu>
Wed, 4 Aug 2021 15:21:50 +0000 (09:21 -0600)
include/deal.II/base/aligned_vector.h

index 52cfb401d0afd22eb9c56d5601d20554f5b6e7ef..8f82b92b6039ec4b503e2a590706da46f505d770 100644 (file)
@@ -1888,42 +1888,13 @@ template <class T>
 inline void
 AlignedVector<T>::swap(AlignedVector<T> &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<T>::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<T>::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);
 }

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.