[Cython] Function calls and ref counts

John Ehresman jpe at wingware.com
Tue Mar 15 15:56:35 EDT 2016


Hi,

I ran into a bug where a python object was deleted in the middle of a 
function that was using it because a callback decref'd the one and only 
reference to the object.  This doesn't happen with python bytecode 
because ref counts of the callable and of all args are essentially 
incref'd before the call and then decref'd after.  Doing the same in 
Cython generated C code might be the way to fix this.

The pure python mode code below reproduces the crash with cython 0.23 
when compiled to C and the Crash function called.

Thanks,

John


import cython

@cython.cclass
class Record:
   cython.declare(ref=object)

   def setref(self, ref):
     self.ref = ref

GLOBAL_RECORD = Record()

@cython.cclass
class CrashCls:

   def method(self):

     cython.declare(rec=Record)

     rec = GLOBAL_RECORD

     print id(self)
     rec.ref = None
     assert isinstance(self, CrashCls)  # <-- should crash

def Crash():
   cython.declare(rec=Record)

   rec = GLOBAL_RECORD

   o = CrashCls()
   rec.ref = o.method
   del o

   args = ()
   rec.ref(*args)



More information about the cython-devel mailing list