[Pyrex] first post, ref count problem

Greg Ewing greg.ewing at canterbury.ac.nz
Wed Jan 31 22:01:14 UTC 2007


leonardo wrote:

> As you can see I've had to use void *pdata to allow it
> to accept items with Python objects references inside.

The simplest and safest way to deal with this is to
make your tyitem an extension type rather than a
struct:

   cdef class tyitem:
     cdef object pl
     cdef object pitem
     cdef object pr

and let Pyrex handle all the reference counting.
I'd strongly recommend this unless there's a *very*
good reason to do otherwise.

As for why your current code doesn't work, I don't
really know, but all those casts to and from object
worry me a bit -- I'd be checking the generated C
code to make sure they weren't introducing any
extra increfs or decrefs.

BTW, your loop for freeing the list elements looks
more complicated than it needs to be. You should
be able to get by with just freeing one element
each time round, i.e.

         while p2 != NULL:
             p3 = p2.pr
             Py_DECREF(<object>p2.pdata)
             PyMem_Free(p2)
             p2 = p3

But if you make tyitem an extension type, all this
will go away.

--
Greg



More information about the Pyrex mailing list