[coco] Deallocate Op in OpManager properly (#2500)
author박종현/동작제어Lab(SR)/Staff Engineer/삼성전자 <jh1302.park@samsung.com>
Thu, 6 Dec 2018 00:23:02 +0000 (09:23 +0900)
committerGitHub Enterprise <noreply-CODE@samsung.com>
Thu, 6 Dec 2018 00:23:02 +0000 (09:23 +0900)
The current implementation of OpManager just deallocates all the Op in
sequential order without considering their dependencies.

However, this implementation may result in USE-AFTER-FREE errors.

This commit guarantees that OpManager deallocates Op nodes from the root
to leaves.

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

index 95769f9..5f4f33c 100644 (file)
@@ -37,6 +37,9 @@ public:
   OpManager(Module *m = nullptr) { module(m); }
 
 public:
+  ~OpManager();
+
+public:
   template <typename T> T *create(void);
 
 public:
index ad20375..3911eb6 100644 (file)
 
 #include <cassert>
 #include <queue>
+#include <set>
 
 using nncc::foundation::make_unique;
 
 namespace coco
 {
 
+OpManager::~OpManager()
+{
+  std::set<coco::Op *> roots;
+
+  for (uint32_t n = 0; n < size(); ++n)
+  {
+    auto op = at(n);
+
+    if (op->up() != nullptr)
+    {
+      continue;
+    }
+
+    roots.insert(op);
+  }
+
+  for (const auto &op : roots)
+  {
+    destroy_all(op);
+  }
+}
+
 //
 // Each Op class SHOULD be default constructible
 //