From a6e8fe6c701010919f5d616efb9b91af3e588328 Mon Sep 17 00:00:00 2001
From: Tianqi Chen <tqchen@users.noreply.github.com>
Date: Sat, 10 Mar 2018 09:40:52 -0800
Subject: [PATCH] [FFI] Fix global free destruction (#985)

---
 python/tvm/_ffi/_ctypes/function.py | 2 +-
 python/tvm/_ffi/_ctypes/node.py     | 3 ++-
 2 files changed, 3 insertions(+), 2 deletions(-)

diff --git a/python/tvm/_ffi/_ctypes/function.py b/python/tvm/_ffi/_ctypes/function.py
index ec278bc20..189d9964b 100644
--- a/python/tvm/_ffi/_ctypes/function.py
+++ b/python/tvm/_ffi/_ctypes/function.py
@@ -165,7 +165,7 @@ class FunctionBase(object):
         self.is_global = is_global
 
     def __del__(self):
-        if not self.is_global:
+        if not self.is_global and _LIB is not None:
             check_call(_LIB.TVMFuncFree(self.handle))
 
     def __call__(self, *args):
diff --git a/python/tvm/_ffi/_ctypes/node.py b/python/tvm/_ffi/_ctypes/node.py
index 08efc3913..cb32b8329 100644
--- a/python/tvm/_ffi/_ctypes/node.py
+++ b/python/tvm/_ffi/_ctypes/node.py
@@ -44,7 +44,8 @@ class NodeBase(object):
         self.handle = handle
 
     def __del__(self):
-        check_call(_LIB.TVMNodeFree(self.handle))
+        if _LIB is not None:
+            check_call(_LIB.TVMNodeFree(self.handle))
 
     def __getattr__(self, name):
         ret_val = TVMValue()
-- 
GitLab