[C++-sig] Py++: Memory Ownership Passing
Anand, Kumar
kanand at qualcomm.com
Wed Feb 21 23:26:34 CET 2007
Ok, I have tried the following but it doesn't work.
1) Exposed class 'Base' as follows:
bp::class_< Base, std::auto_ptr< Base > > ("Base" )
.def( bp::init< >() );
1) Exposed class 'Derived' as follows:
bp::class_< Derived, bp::bases< Base >, std::auto_ptr<
Derived > > ("Derived" )
.def( bp::init< >() );
2) Created the func_wrapper as follows and exposed it to python as
'func'
Void func_wrapper (std::auto_ptr<Base> b)
{
func.(b.get());
b.release()
}
Now In Python when I do this:
import Boost_Test
d = Boost_Test.Derived()
Boost_Test.func(d);
Above code throws an error because of C++ signature mismatch. I am
trying to pass a std::auto_ptr<Derived>to a std::auto_ptr<Base>. How to
solve this problem?
Thanks
Kumar
-----Original Message-----
From: c++-sig-bounces at python.org [mailto:c++-sig-bounces at python.org] On
Behalf Of Roman Yakovenko
Sent: Wednesday, February 21, 2007 12:55 PM
To: Development of Python/C++ integration
Subject: Re: [C++-sig] Py++: Memory Ownership Passing
On 2/21/07, Anand, Kumar <kanand at qualcomm.com> wrote:
> How can I pass the memory ownership of a Derived class object (created
in
> Python) to C++ though an interface that takes a Base class pointer?
Does the
> function transformer in Py++ tackle this use case?
>
> ----------------------------------------------------------
>
> Scenario: There is a class 'Base' and a derived class 'Derived' that
extends
> from 'Base'. Both the classes have been exposed to Python.
>
> Class Base {};
>
> Class Derived: public Base {};
>
> bp::class_< Base > ("Base " )
>
> .def( bp::init< >() );
>
> bp::class_< Derived, bp::bases< Base > > ("Derived " )
>
> .def( bp::init< >() );
>
> Now I have a C++ function f that has also been exposed to python:
>
> void func (Base class * ptr)
>
> {
>
> //this function takes memory ownership of the passed object and
does say
> delete ptr;
>
> }
>
> Assume all the above bindings have been exposed in the Boost_Test
module and
> I have the following python code:
>
> import Boost_Test
>
> d = Boost_Test.Derived()
>
> Boost_Test.func(d);
>
> //This finally causes the interpreter to crash because of double
deletion of
> object 'd'
>
> ---------------------------------------------------------------
>
> Specifying the 'held_type' for Derived class as std::auto_ptr and
writing a
> wrapper function for 'func' that takes a std::auto_ptr<Base> as
argument
> does not work.
Please read this thread:
https://sourceforge.net/mailarchive/forum.php?thread_id=31679293&forum_i
d=47898
It deals with almost identical situation. You should be able to find the
answers
to your questions in it.
Also take a look on "transfer_ownership" function transformation
documentation:
http://language-binding.net/pyplusplus/documentation/functions/transform
ation/built_in/transfer_ownership.html
--
Roman Yakovenko
C++ Python language binding
http://www.language-binding.net/
_______________________________________________
C++-sig mailing list
C++-sig at python.org
http://mail.python.org/mailman/listinfo/c++-sig
More information about the Cplusplus-sig
mailing list