From 00f4630d41d72bde9b3d411c3e505ad435f1c3c9 Mon Sep 17 00:00:00 2001
From: Christian Engwer <christi@dune-project.org>
Date: Fri, 25 Jan 2019 12:13:15 +0100
Subject: [PATCH] [amg] fix weak_ptr issue when copying a hierarchy

---
 dune/istl/paamg/hierarchy.hh | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)

diff --git a/dune/istl/paamg/hierarchy.hh b/dune/istl/paamg/hierarchy.hh
index 1bffc651e..c705aedd9 100644
--- a/dune/istl/paamg/hierarchy.hh
+++ b/dune/istl/paamg/hierarchy.hh
@@ -1243,11 +1243,14 @@ namespace Dune
       }
       finest_ = std::allocate_shared<Element>(allocator_);
       std::shared_ptr<Element> finer_;
-      std::shared_ptr<Element> current_      = finest_;
-      std::shared_ptr<Element> otherCurrent_ = other.finest_;
+      std::shared_ptr<Element> current_ = finest_;
+      std::weak_ptr<Element> otherWeak_ = other.finest_;
 
-      while(otherCurrent_)
+      while(! otherWeak_.expired())
       {
+        // create shared_ptr from weak_ptr, we just checked that this is safe
+        std::shared_ptr<Element> otherCurrent_ = std::shared_ptr<Element>(otherWeak_);
+        // clone current level
         #warning should we use the allocator?
         current_->element_ =
           std::make_shared<MemberType>(*(otherCurrent_->element_));
@@ -1262,7 +1265,8 @@ namespace Dune
           current_->coarser_ = c;
           current_ = c;
         }
-        otherCurrent_ = std::shared_ptr<Element>(otherCurrent_->coarser_);
+        // go to coarser level
+        otherWeak_ = otherCurrent_->coarser_;
       }
       coarsest_=current_;
     }
-- 
GitLab