I am not entirely clear how memory management works in cython for 
extension classes and their attributes/members.

I have a class in a .pxd file defined like,

cdef class rmat :
     """  Real matrix class binds to both vsipl and numpy ndarray.
     The ndarray object is returned via getarr.  The array is bound
     back to vsipl via admit """
     cdef vsip_mview_f *vm
     # this should be allowed but currently isn't
     # cdef np.ndarray[np.float32_t, ndim=2] arr
     # cdef vsip_block_f *block
     cdef object arr
     cpdef rmat parent
  ....


So apparently I can create an rmat object in python.  I have 
__cinit__()  and  __dealloc__() methods defined.

Question 1,  are references to an rmat object reference counted and 
garbage collected?
So far it seems like they are.

Question 2)  Are extension class members set to  other  extension 
classes or python objects,  such as  arr and parent  reference counted?
ie suppose I create a real matrix such as,
 >> m = rmat(2,3)
 >> m.arr = array1
 >> m.parent =  m0

The assignments to the class members are actually done inside a .pyx 
file so they are done via cython.  Do such assignments increment the 
refcounts and are things properly garbage collected when the reference 
counts go to zero?

Again it seems like this is true,  but it's not entirely obvious.  I 
might expect  member arr to be properly refcounted since it's explicitly 
declared as a python object,  but the parent member is declared as an 
extension type object.  In theory it could just treat it as a C pointer. 
What if I had declared it via a cdef instead of cpdef,  does that make 
any difference?

  I have some anecdotal evidence that it is properly garbage collected,  
but I don't know the rules or restrictions on this since the 
documentation only talks briefly about these issues in a couple of places.

Thanks in advance for your help,


_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev

Reply via email to