The relationship was something generic, one holds a reference of the other.
I will hold a Python reference to let python handle all the reference
counting, after all it isn't so bad!

Thank you very much.

- Gabriele

2010/4/18 Stefan Behnel <[email protected]>

> Gabriele Lanaro, 18.04.2010 22:19:
> > 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
>
> Not at all.
>
>
> > Are there any best practice to handle this kind of situations?
>
> You have two choices: let A handle all the memory allocation, or keep a
> Python reference from A to the B instances it references. You were not very
> explicit about the meaning and the relationship of the two structs, so
> depending on what they do and why they do it, either of the two solutions
> can be better.
>
> Stefan
> _______________________________________________
> Cython-dev mailing list
> [email protected]
> http://codespeak.net/mailman/listinfo/cython-dev
>
_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev

Reply via email to