From 727c67b4c7f9a8abf22a6d88fdd4c96e7d3f43c8 Mon Sep 17 00:00:00 2001
From: Botond Baranyi <botond.baranyi@ericsson.com>
Date: Tue, 19 May 2020 14:03:18 +0200
Subject: [PATCH] Fixed infinite loop when creating references to an object in
 its destructor (bug 552011)

Change-Id: I17616dcccc69f05742c050c3ba4a2b686617be96
Signed-off-by: Botond Baranyi <botond.baranyi@ericsson.com>
---
 core/OOP.hh                  | 10 ++++++++--
 regression_test/oop/oop.ttcn |  4 ++++
 2 files changed, 12 insertions(+), 2 deletions(-)

diff --git a/core/OOP.hh b/core/OOP.hh
index 52f2c223b..4dced3ffa 100644
--- a/core/OOP.hh
+++ b/core/OOP.hh
@@ -22,12 +22,14 @@
 class OBJECT {
 private:
   size_t ref_count;
+  boolean destructor; // true, if the destructor is currently running;
+  // also makes sure the object is not deleted again when inside the destructor
   
   OBJECT(const OBJECT&); // copy disabled
   OBJECT operator=(const OBJECT&); // assignment disabled
   boolean operator==(const OBJECT&); // equality operator disabled
 public:
-  OBJECT(): ref_count(0) {}
+  OBJECT(): ref_count(0), destructor(FALSE) {}
   virtual ~OBJECT() {
     if (ref_count != 0) {
       TTCN_error("Internal error: deleting an object with %lu reference(s) left.", ref_count);
@@ -36,7 +38,11 @@ public:
   void add_ref() { ++ref_count; }
   boolean remove_ref() {
     --ref_count;
-    return ref_count == 0;
+    if (destructor) {
+      return FALSE;
+    }
+    destructor = ref_count == 0;
+    return destructor;
   }
   virtual void log() const {
     TTCN_Logger::log_event_str("object: { }");
diff --git a/regression_test/oop/oop.ttcn b/regression_test/oop/oop.ttcn
index d16a79e01..ffd2a2015 100644
--- a/regression_test/oop/oop.ttcn
+++ b/regression_test/oop/oop.ttcn
@@ -101,6 +101,10 @@ type class BaseClass runs on CT mtc CT system CT {
 finally {
   //pt.send(-1);
   log(m_var);
+  log(this);
+  log(this);
+  log(this);
+  log(this);
 }
 
 type class SubClass extends BaseClass {  
-- 
GitLab