[C++-sig] Cycles won't be deleted

David Abrahams dave at boost-consulting.com
Thu Jan 5 02:49:25 CET 2006


bob at glumol.com writes:

> Hello,
>
> I'm sorry if this question has already been asked but I didn't find an
> answer on the archives.
> With the Python code :
>
> class C: pass
>
> c1 = C()
> c2 = C()
> c1.child = c2
> c2.parent = c1
> del c1
> del c2
> import gc
> gc.collect()
>
> gc.collect() will return 2.
> Now if C is not a pure Python class but a BPL one, gc.collect() will
> return 0 and the 2 objects seem to never be deleted.
> I can define 'child' and 'parent' as members of the C++ class (using a
> shared_ptr and a weak_ptr, that works) but if an other cycle is defined in
> Python, the objects won't be deleted.
>
> Could someone explain me why such a behavior ?

Because there is no way, in general, to traverse all the references to
Python objects that may be embedded in (or otherwise owned by) a given
C++ object, Boost.Python marks all wrapped C++ objects as "not
GC-able."  If we didn't do that, you would see objects disappearing
too early, which is much worse than a leak.

-- 
Dave Abrahams
Boost Consulting
www.boost-consulting.com




More information about the Cplusplus-sig mailing list