[C++-sig] new to python wrapping...

David Abrahams david.abrahams at rcn.com
Tue Jan 22 13:39:41 CET 2002


----- Original Message -----
From: "Hugo van der Merwe" <s13361562 at bach.sun.ac.za>


> > No; it took me a while to understand this myself, but Python uses a
> > different sort of GC which is more conservative. Instead of collecting
> > everything it can't prove is referenced, it looks for reference cycles
that
> > it /can/ find, and which it can prove are not referenced from outside
the
> > cycle (it can do that by looking at the reference counts).
>
> Not sure I followed that 100%. Here is how I understand it at the
> moment, quite possibly incorrectly, correct me where I go wrong:
>
> It uses refcounts, right, so if you do:
>
> a = NewClass()
>
> the instance of NewClass's refcount just went up by one. Then I do
>
> someCPPClass.Add(a)
>
> This puts the instance mentioned above in some list. If I now reassign
> the python "a" to something else, e.g.:
>
> a = 0
>
> The refcount for the instance of NewClass will go down by one again,
> won't it? In which case it will be disposed? Unless the refcount is
> increased by the someCPPClass.Add(a) call, something that Boost.Python
> will have to take care of, if I'm correct so far.
>
> Where am I going wrong? Or does Boost.Python take care of this? Or will
> there be a GC problem? I have not yet tested this. Maybe I should some
> time soon.

If you don't traffic in raw PyObject*s, but instead, you use
boost::python::reference<PyObject>s, Boost.Python will take care of it.





More information about the Cplusplus-sig mailing list