[C++-sig] shared_ptr and register_ptr_to_python

Marek Denis marek at octogan.net
Sun Oct 10 23:16:59 CEST 2010


On 10.10.2010 20:15, Jim Bosch wrote:

> Actually, you want something like this:
>
> register_ptr_to_python< boost::shared_ptr<A> >();
>
> If you want to use raw pointers, I don't think you need
> register_ptr_to_python (see below).


You are right.

> I'm actually kind of surprised about this - in other circumstances
> (particularly when returning values in wrapped functions by pointer),
> Boost.Python doesn't copy pointers or references unless you explicitly
> ask it to.

I did some tests to check it out (you can check the code at [1] )
and noticed, that when I launch the
+v
boost::python::call_method<void>(callback,"addToQueue",c);
-v

piece of code, the object is copied after using self.queue.get() in the 
python script. This may be the magic (and/or implementation of the 
Queue.Queue) of Python layer, not Boost.Python itself.
So I believe I will have to live with the pointless (in this case) 
memory copying in my program :)

> One thing that may be worth trying: when you wrap C Python,
> mark it as noncopyable:
>
> class_<C,bases<B>,boost::noncopyable>("C")
>
> I'd be curious to see what happens with your call_method code snippet
> with that in place.

I will let you know when I find somthing out.

[1] http://marek.octogan.net/python/

Using raw pointers is enough in my case, shared_ptrs would be safer, as 
different threads may delete object.

-- 

Marek Denis



More information about the Cplusplus-sig mailing list