|
Boost : |
From: Niall Kelly (ngktab_at_[hidden])
Date: 2002-07-31 21:43:42
Doug,
I have been using Boost.Signals for some time, often
in the scenario where the connected slots are member
functions. In order to handle the case where an object
(which has one of its methods connected to some
signal) is deleted, I wanted to use the
boost::signals:::trackable class.
I initially thought that this class was not behaving
as documented. See code fragment:
class AClass : public boost::signals::trackable
{
...
void AMethod(unsigned id);
...
};
typedef boost::signal<void, unsigned> tSigType;
typedef tSigType::slot_type tSlotType;
tSigType sig;
AClass* pAInst = new AClass;
boost::function1<void, unsigned> f =
boost::bind(&AClass::AMethod, pAInst, _1);
sig.connect(f);
sig(3);
The code above happily compiles and runs as expected.
However, if pAInst is deleted at some point the bound
method remains connected to the signal.
I then realised that rather than assigning the result
of the bind to the boost::function f, I should assign
it to a tSlotType variable which is then passed to
sig.connect(). In this case, the trackable behaviour
works as documented - if I delete pAInst, the slot is
disconnected as expected.
My concern is that it seems reasonable to pass a
boost::function to the signal's connect (just as it is
reasonable to pass a plain old C function) and yet the
behaviour is quite different to the case where the
slot is explicitly connected as a slot_type.
Regards,
Niall.
http://digital.yahoo.com.au - Yahoo! Digital How To
- Get the best out of your PC!
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk