[C++-sig] Re: Boost::python and shared_ptr

David Abrahams dave at boost-consulting.com
Wed Jan 5 00:11:52 CET 2005


Bernhard Glück wrote:
>   Hi !
> 
> I am using a boost::python to script our newest project.
> We use boost::shared_ptr for almost all of our objects.
> 
> How do i correctly wrap my classes so that they are held as shared_ptr 
> objects and
> passed around that way ? I tried doing it like it was posted this 
> mailing list
> before, but with the following problems.
> 
> My environment is as follows::
> 
> Python 2.4,
> Boost 1.32.0
> Compiler: VC7.1
> 
> Here the relevant code passages:
> 
> First our game object which holds smart pointers to several sub systems:

Please try to cut down your examples to something more manageable in the
future, if possible.

>      register_ptr_to_python< boost::shared_ptr<ConfigurationServer> >();
> 
>      class_< ConfigurationServer,boost::shared_ptr<ConfigurationServer> >
> ("ConfigurationServer",no_init )


You don't need both of these.  The to-python conversion for
boost::shared_ptr<ConfigurationServer> is automatically registered by
the class_<...> declaration.

> The problem now is that the game class works as expected but whenever i do
> something like this:
> 
> import dv
> game = dv.getGame()
> config = game.getConfigurationServer()
> config.set("Graphics.Fullscreen",1)
> 
> I get the following error:
> 
> exceptions.TypeError
> 20:54:20: No Python class registered for C++ class class
> boost::shared_ptr<class
> DeepVoid::ConfigurationServer>
> 20:54:20:   File "DeepVoidConfig.py", line 3, in ?     config =
> game.getConfigurationServer()

All I can guess is that PyExportConfigurationServer hasn't been executed
yet.  Try setting a breakpoint there.

> I have no clue why this could happen. I  tried chaning the export code 
> for the
> ConfiguratioNServer in various ways ( leaving out register_ptr_to_python,
> or the second wrapper definition ) to no avail..
> 
> Both classes get exported by the way ( as my log tells me, thats why i have
> those two Log statements in the code )....

Oh.  Hm.

Err.... why are you using this return value policy?

  .def("getConfigurationServer",&Game::GetConfigurationServer,
       return_value_policy<reference_existing_object>());

That's your problem.  I suggest you go back and review the meaning of
these policies, and post any questions you have to this list to make
sure you understand them.


-- 
Dave Abrahams
Boost Consulting
http://www.boost-consulting.com




More information about the Cplusplus-sig mailing list