[C++-sig] Ownership of a C++ object extended in Python using wrapper

Neal Becker ndbecker2 at gmail.com
Tue Mar 16 13:12:21 CET 2010


Alexandre Hamez wrote:

> Hi all,
> 
> I have a C++ library that needs to store in a hash map user's code, that
> is, Python-extended objects. To make the C++ interface available to
> Python, I use boost::python::wrapper, as stated in the tutorial
> 
(http://www.boost.org/doc/libs/1_42_0/libs/python/doc/tutorial/doc/html/python/exposing.html#python.class_virtual_functions).
> Because I store this extend objects, I need to tell Python that I've got a
> reference on these objects.
> A thread from 2007
> (http://mail.python.org/pipermail/cplusplus-sig/2007-March/011790.html)
> states there is a problem for this particular use case, but in the end, no
> solution is given. So my question is the following: as of today, what is
> the correct way to  tell Python we hold a reference to Python extend
> objects of C++ classes, using the boost::python::wrapper mechanism?
> 
> Thanks,
> ---------------------
> Alexandre Hamez

I've use 2 methods:

1) with_custodian_and_ward
2) boost::shared_ptr



More information about the Cplusplus-sig mailing list