From 95f0a028bc49888df1c77734be778ab570b9db34 Mon Sep 17 00:00:00 2001
From: joanvallve <jvallve@iri.upc.edu>
Date: Tue, 21 Dec 2021 14:03:36 +0100
Subject: [PATCH] hotfix: PT patch avoiding implicit destruction of last

---
 include/core/capture/capture_base.h |  2 ++
 src/capture/capture_base.cpp        | 31 ++++++++++++++++++-----------
 src/processor/processor_tracker.cpp |  2 +-
 3 files changed, 22 insertions(+), 13 deletions(-)

diff --git a/include/core/capture/capture_base.h b/include/core/capture/capture_base.h
index 0e9e606b5..9a32069c1 100644
--- a/include/core/capture/capture_base.h
+++ b/include/core/capture/capture_base.h
@@ -114,6 +114,8 @@ class CaptureBase : public NodeBase, public HasStateBlocks, public std::enable_s
 
         void move(FrameBasePtr);
         void link(FrameBasePtr);
+        void unlink();
+
         template<typename classType, typename... T>
         static std::shared_ptr<classType> emplace(FrameBasePtr _frm_ptr, T&&... all);
 
diff --git a/src/capture/capture_base.cpp b/src/capture/capture_base.cpp
index c234fafdc..811f1b3bb 100644
--- a/src/capture/capture_base.cpp
+++ b/src/capture/capture_base.cpp
@@ -210,34 +210,41 @@ void CaptureBase::move(FrameBasePtr _frm_ptr)
     assert((this->getFrame() == nullptr || not this->getFrame()->getProblem()) && "Forbidden: trying to move a capture already linked to a KF!");
 
     // Unlink
-    if (this->getFrame())
-    {
-        // unlink from previous non-key frame
-        this->getFrame()->removeCapture(shared_from_this());
-        this->setFrame(nullptr);
-    }
+    unlink();
 
     // link
     link(_frm_ptr);
 }
 
-
 void CaptureBase::link(FrameBasePtr _frm_ptr)
 {
     assert(!is_removing_ && "linking a removed capture");
     assert(this->getFrame() == nullptr && "linking a capture already linked");
 
+    WOLF_WARN_COND(_frm_ptr == nullptr, "Linking Capture ", id(), " to a nullptr");
+
     if(_frm_ptr)
     {
-
         _frm_ptr->addCapture(shared_from_this());
         this->setFrame(_frm_ptr);
         this->setProblem(_frm_ptr->getProblem());
     }
-    else
-    {
-        WOLF_WARN("Linking Capture ", id(), " to a nullptr");
-    }
+}
+
+void CaptureBase::unlink()
+{
+    WOLF_WARN_COND(this->getFrame() == nullptr, "Unlinking a not linked Capture ", id(), ". Nothing to do, skipping...");
+
+    if (not this->getFrame())
+        return;
+
+    for (auto ftr : getFeatureList())
+        assert(ftr->getFactorList().empty() && " unlinking a capture with factors!");
+    assert(getConstrainedByList().empty() && " unlinking a capture constrained by factors!");
+
+    // unlink from frame
+    this->getFrame()->removeCapture(shared_from_this());
+    this->setFrame(nullptr);
 }
 
 void CaptureBase::setProblem(ProblemPtr _problem)
diff --git a/src/processor/processor_tracker.cpp b/src/processor/processor_tracker.cpp
index 32c74f2db..f8f6c598c 100644
--- a/src/processor/processor_tracker.cpp
+++ b/src/processor/processor_tracker.cpp
@@ -252,7 +252,7 @@ void ProcessorTracker::processCapture(CaptureBasePtr _incoming_ptr)
                                                                  getProblem()->getFrameStructure(),
                                                                  last_ptr_->getFrame()->getState());
                 incoming_ptr_->link(frame);
-                last_ptr_->getFrame()->remove(); // implicitly calling last_ptr_->remove();
+                last_ptr_->unlink(); // unlink last (destroying the frame) instead of frame destruction that would implicitly destroy last
 
                 // Update pointers
                 last_ptr_       = incoming_ptr_;
-- 
GitLab