From 9a08ff23544ac9afd8392715c660898e1f3a762f Mon Sep 17 00:00:00 2001 From: Robert Bradshaw Date: Wed, 2 Jan 2013 14:15:23 -0800 Subject: [PATCH] Guard deallocation with PyObject_GC_Untrack, trac #796. --- Cython/Compiler/ModuleNode.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/Cython/Compiler/ModuleNode.py b/Cython/Compiler/ModuleNode.py index a3e1a48..ed64e70 100644 --- a/Cython/Compiler/ModuleNode.py +++ b/Cython/Compiler/ModuleNode.py @@ -1127,6 +1127,11 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode): or memoryview_slices or weakref_slot in scope.var_entries): self.generate_self_cast(scope, code) + + # We must mark ths object as (gc) untracked while tearing it down, lest + # the garbage collection is invoked while running this destructor. + if scope.needs_gc(): + code.putln("PyObject_GC_UnTrack(o);"); # call the user's __dealloc__ self.generate_usr_dealloc_call(scope, code) @@ -1151,6 +1156,11 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode): code.put_xdecref_memoryviewslice("p->%s" % entry.cname, have_gil=True) + # The base class deallocator probably expects this to be tracked, so + # undo the untracking above. + if scope.needs_gc(): + code.putln("PyObject_GC_Track(o);"); + if base_type: tp_dealloc = TypeSlots.get_base_slot_function(scope, tp_slot) if tp_dealloc is not None: -- 2.7.4