I've a C where some relationship between "object" exists, like this
pseudocode:

c_structA {
  c_structB *bstructs;
}

c_structB ...

I've defined some methods to handle the memory management of these
structures:

new_c_structA
new_c_strucB
free_c_structA
free_c_structB

And also other functions (mehods) like:
c_structA_append(c_structA *a, c_structB *b)


I've to wrap these structures in cython but I encounter some problems with
the reference counting and the garbage collector.


class A:
   cdef c_structA *this

   def append(self, b):
       c_structA_append(self.this, b.this)

class B:
   ...


My problem is in particular, when I call append from the class A, the
reference count of b doesn't increment (since the aggregation is done in C),
so b is garbage collected and I lost any reference of c_structB (b.this)
causing a segfault when accessing it. One solution would be for example
this:

   def append(self, b):
       c_structA_append(self.this, b.this)
       self.blist.append(b)

However it seems to be a dirty solution that brings some code duplication,
what do you think about it?

Are there any best practice to handle this kind of situations?

( I'm sorry for the bad example and bad naming I choose, if you don't
understand the question I can rewrite it).

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

Reply via email to