[C++-sig] call_method

David Abrahams dave at boost-consulting.com
Sun Nov 10 05:18:08 CET 2002


"Brett Calcott" <brett.calcott at paradise.net.nz> writes:

> Hiya Tim,
>
>
>>
>> My (relatively uninformed) guess is that the inheritance/call-back issue
>> is a red herring -- I think the agent is getting garbage-collected
>> before you call 'do_something' on it.  I reckon you should just replace
>>
>>   .def("add_agent", &engine::add_agent)
>>
>> with
>>
>>   .def("add_agent", &engine::add_agent, with_custodian_and_ward<1, 2>())
>>
>> so that the lifetime of the agent object is linked to that of the
>> engine.  I could be wrong, of course (has been known to happen)...
>>
>>
>> Hope that helps,
>
> Yup, that helps a lot. So much so that it actually works now :)
>
> Whew...
>
> I had assumed that since Boost.Python passed me the PyObject *self, that the
> lifetime of the PyObject was already bound to my C++ object. That is what I
> actually want, as binding the agent to the engine is only safe if I the C++
> engine is responsible for the lifetime of the C++ agent (actually a
> reasonable assumption is this case). Of course doing something like that
> requires cyclic dependencies as the python object holds a shared_ptr to the
> C++ object. Hmmm. How hard would that be...
>
>
> Thanks Tim,

Yeah, Tim, thanks for your sharp analysis! Too bad I didn't read it
before writing the foregoing diatribe!

-- 
                       David Abrahams
   dave at boost-consulting.com * http://www.boost-consulting.com
Boost support, enhancements, training, and commercial distribution





More information about the Cplusplus-sig mailing list