diff --git a/src/api/python/z3/z3.py b/src/api/python/z3/z3.py index 2bc055b6f..94411aa75 100644 --- a/src/api/python/z3/z3.py +++ b/src/api/python/z3/z3.py @@ -301,8 +301,9 @@ class AstRef(Z3PPObject): Z3_inc_ref(self.ctx.ref(), self.as_ast()) def __del__(self): - if self.ctx.ref() is not None: + if self.ctx.ref() is not None and self.ast is not None: Z3_dec_ref(self.ctx.ref(), self.as_ast()) + self.ast = None def __deepcopy__(self, memo={}): return _to_ast_ref(self.ast, self.ctx)