boost::python::return_value_policy<bp::reference_existing_object>() is weakref in python?

I've used boost::python in the past, but this problem has me stumped. Here's part of the class I'm having issues with: class Entity { public: Entity(); ~Entity(); //Call to check our collider against all entities that have "tag" Collider* Collide(const std::string &tag); }; and here is the boost::python code: bp::class_< Entity_wrapper >( "Entity", bp::init< >() ) .def( "Collide" , (::Monocle::Collider * ( ::Monocle::Entity::* )( ::std::string const & ) )( &::Monocle::Entity::Collide ) , ( bp::arg("tag") ), bp::return_value_policybp::reference_existing_object() ) In python if I do something like this: collider = entity.Collision("Paddle") collider is a weakref to 'type' Not a Collider. My other functions that return pointers seem to be fine. Is it just the call policy I'm using? What Collide does is call a static function ("Collide") of the Collision class, which in turn returns a Collider* on success and NULL otherwise. Any help would be appreciated! If you need anymore information, let me know! -- Tom Rab

On Sat, Mar 19, 2011 at 12:39 AM, Thomas Rab
::Monocle::Collider * ( ::Monocle::Entity::* )( ::std::string const & ) )( &::Monocle::Entity::Collide )
I'm not sure of it, but the problem might be here. What happens if you pass plain &::Monocle::Entitiy::Collide here instead (and why on earth are you using a C-style cast here in the first place?) -- Dave Abrahams BoostPro Computing http://www.boostpro.com

On Sat, Mar 19, 2011 at 12:39 AM, Thomas Rab
wrote: ::Monocle::Collider * ( ::Monocle::Entity::* )( ::std::string const & ) )( &::Monocle::Entity::Collide )
I'm not sure of it, but the problem might be here. What happens if you pass plain
&::Monocle::Entitiy::Collide
here instead (and why on earth are you using a C-style cast here in the first place? )
The boost::python code was generated with py++ (should have mentioned
On Tue, Mar 22, 2011 at 2:07 AM, Dave Abrahams

At Tue, 22 Mar 2011 02:40:41 -0500, Thomas Rab wrote:
--00151747c2d07665e8049f0d5c00 Content-Type: text/plain; charset=ISO-8859-1
On Tue, Mar 22, 2011 at 2:07 AM, Dave Abrahams
wrote: On Sat, Mar 19, 2011 at 12:39 AM, Thomas Rab
wrote: ::Monocle::Collider * ( ::Monocle::Entity::* )( ::std::string const & ) )( &::Monocle::Entity::Collide )
I'm not sure of it, but the problem might be here. What happens if you pass plain
&::Monocle::Entitiy::Collide
here instead (and why on earth are you using a C-style cast here in the first place? )
The boost::python code was generated with py++ (should have mentioned that). I did as you suggested, but I get the same results, unfortunately. Here's what print collider yields in python:
The other thing which may attribute to the problem is that Collider is noncopyable, but I would expect a different result if that was the case.
The only place weakrefs can come from in Boost.Python are from the use of one of these two policies: return_internal_reference with_custodian_and_ward Since I don't see either one in your code, I can only conclude that you're not looking where the problem is, and that (as usual with Boost.Python) if you reduce your problem to an absolute minimal reproducible case, you will solve it along the way. If you don't, feel free to re-post that example so that I can debug it locally. -- Dave Abrahams BoostPro Computing http://www.boostpro.com
participants (2)
-
Dave Abrahams
-
Thomas Rab