On 05/07/12 11:49, Jani Tiainen wrote:
> Hi,
> 
> I'm new to python.boost library and I'm trying to use it to wrap a third
> party library. Everything else I've managed to get working - thanks to
> excellent library and lot of examples I've found around a net.
> 
> Only thing I can't get working properly is instance ownership transfer
> in constructor.
> 
> So classes in library are defined like follows:
> 
> class Owner {
> ...
> }
> 
> class Data {
>     Data(Owner *owner); // <-- Here happens ownership transfer.
> }
> 
> 
> Python code is like follows:
> 
> owner = Owner()
> data_1 = Data(owner)
> data_2 = Data(owner)
> 
> So when Python script stops runnning it causes crash due the fact that
> I've data objects are automatically destroyed by Owner-class but Python
> deletes them by using Data-class destructor (which shouldn't happen ever).
> 

If I understand you correctly then you want the owner object to remain
alive at least as long as data_1 and data_2? If so you could use
with_custodian_and_ward:
http://www.boost.org/doc/libs/1_49_0/libs/python/doc/v2/with_custodian_and_ward.html

For example something like the following should work:

        namespace py = boost::python;
        py::class_<
                ...
        > my_class(
            "Data",
            "docstring.",
            py::init< Owner * >( "Constructor." )[
                py::with_custodian_and_ward< 1, 2 >()
            ]
        );

_______________________________________________
Cplusplus-sig mailing list
Cplusplus-sig@python.org
http://mail.python.org/mailman/listinfo/cplusplus-sig

Reply via email to