[C++-sig] call_method

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


Tim Field <nobbis at mcs.vuw.ac.nz> writes:

> On Sun, 2002-11-10 at 00:59, Brett Calcott wrote:
>
> <snip>
>
>> The next one returns with a C++ runtime exception error
>> Debugging the code it looks as if the m_self member that is held in the
>> callback class has been deleted - causing the crash.
>
> Hey Brett,
>
> 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)...

That's an ingenious thought... 

One caveat: the agent will never get destroyed until the engine does,
even if you erase it from the vector.

-- 
                       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