[C++-sig] Custom smart pointer with same behaviour as shared_ptr

Andrea mariofutire at googlemail.com
Wed Jan 23 21:34:36 CET 2008


john haddon wrote:
> 
> the problem is this - say you bind this function :
> 
> intrusive_ptr x( intrusive_ptr b ) { return b; }
> 
> then this python code fails :
> 
> b = B()
> b2 = x( b )
> assert( b is b2 ) # fails
> 
> although both b and b2 correctly refer to the same c++ object, they refer to two different python
> instances - what x should really do is return the same PyObject that was being used to hold b in the
> first place. i believe there's some magic in boost::python that solves this problem as long as you're
> using shared_ptr (although you might want to test that), but the patch i posted for the downcasting
> of intrusive_ptr doesn't address it...

Yes, boost::shared_ptr<> achieve this

 >>> b=bo.newB()
 >>> c=bo.sameB(b)
 >>> b
<bo.B object at 0xb7e9102c>
 >>> c
<bo.B object at 0xb7e9102c>
 >>> b is c
True

but any other pointer fails.

It looks that there is some special handling of boost::shared_ptr<> when they are returned to python.

Has anybody found where this happens.
Maybe the fix is as easy allowing the downcasting of custom pointers...

I've tried to follow the function calls, but I got lost after a few layer of templates...

Andrea




More information about the Cplusplus-sig mailing list