[C++-sig] How do I make correct wrappers to interfaces while using shared_ptrs?

Jim Bosch talljimbo at gmail.com
Sat Feb 4 17:46:52 CET 2012


On 02/04/2012 11:11 AM, Adam Preble wrote:
> I am trying to run this on Win32, using Stackless Python 2.6, and Boost
> 1.47.
>
> I am having a problem with passing a shared_ptr of a C++ object
> implementing a C++ interface to a wrapper of a Python object
> implementing a C++ interface.  That's a mouthful, so I thought I'd
> simplify the code.  The thing is, I can't seem to get the wrapping
> correct.  If I can get this going, I can start adding complication to it
> and see what the problem really is.
>
> Basically, I have an interface that works with itself for communicating
> stuff.  There is a C implementation of it, and a Python implementation
> of it.  I have to write a wrapper.  The Python implementation can
> communicate with instances of the C implementation, but not vice versa.
>   When the C implementation calls the Python one, I get:
>
> Traceback (most recent call last):
>    File "<stdin>", line 1, in <module>
> TypeError: No to_python (by-value) converter found for C++ type: class
> boost::shared_ptr<class Communicatable>
>
> The magic call is in the python script.
>
> Python script
> http://pastebin.com/DJ65tKEW
>
> Header
> http://pastebin.com/6BX8e6KA
>
> Source (Boost.Python declarations here)
> http://pastebin.com/s5YC1hyC
>
> I have done similar wrappers like this before so I'm just befuddled
> about why this time it hates me.  The shared_ptrs are part of the larger
> problem I'm trying to reproduce, so I want to incorporate them into this
> test program.  In the larger program, while doing a get_override() call
> on a wrapper to an interface, I get a segfault inside the Python DLLs
> while it tries to convert the shared_ptr.  The last line I can see i
> shared_ptr_to_python.hpp:
>
>          return converter::registered<shared_ptr<T>
> const&>::converters.to_python(&x);
>
> That's what I am eventually hoping to reproduce by shooting back and
> forth all this stuff.
>

If I understand your intend correctly, I think your class_ definition 
needs to be:

class_<Communicable,shared_ptr<CommunicableWrapper>,noncopyable>

(right now you have class_<CommunicableWrapper,...>)



Jim


More information about the Cplusplus-sig mailing list