[coco] Detach Node from List on destruction (#1514)
author박종현/동작제어Lab(SR)/Staff Engineer/삼성전자 <jh1302.park@samsung.com>
Mon, 17 Sep 2018 04:24:30 +0000 (13:24 +0900)
committerGitHub Enterprise <noreply-CODE@samsung.com>
Mon, 17 Sep 2018 04:24:30 +0000 (13:24 +0900)
This commit revises DLinkedList::Node's destructor to detach itself on
destruction.

Signed-off-by: Jonghyun Park <jh1302.park@samsung.com>
contrib/coco/core/include/coco/ADT/DLinkedList.h
contrib/coco/core/src/ADT/DLinkedList.test.cpp

index d506675..3eee697 100644 (file)
@@ -149,7 +149,14 @@ template <typename Child, typename Parent> struct DLinkedList
     }
 
   public:
-    virtual ~Node() = default;
+    virtual ~Node()
+    {
+      if (parent())
+      {
+        // Detach itself on destruction (if it belongs to a list).
+        detach();
+      }
+    }
 
   public:
     Parent *parent(void) const { return _parent; }
index c09ddd8..c97ca69 100644 (file)
@@ -184,3 +184,30 @@ TEST(ADT_DLINKED_LINK, detach)
   delete parent;
   delete link;
 }
+
+TEST(ADT_DLINKED_LINK, node_destructor)
+{
+  auto link = new coco::PtrLink<::Child, ::Parent>{};
+  auto parent = new ::Parent{link};
+
+  auto child_1 = new ::Child{link};
+  auto child_2 = new ::Child{link};
+
+  parent->children()->append(child_1);
+  parent->children()->append(child_2);
+
+  delete child_2;
+
+  ASSERT_EQ(parent->children()->head(), child_1);
+  ASSERT_EQ(parent->children()->tail(), child_1);
+  ASSERT_EQ(child_1->next(), nullptr);
+  ASSERT_EQ(child_1->prev(), nullptr);
+
+  delete child_1;
+
+  ASSERT_EQ(parent->children()->head(), nullptr);
+  ASSERT_EQ(parent->children()->tail(), nullptr);
+
+  delete parent;
+  delete link;
+}