Boost logo

Boost :

From: Frank Mori Hess (frank.hess_at_[hidden])
Date: 2008-03-07 10:30:58


On Friday 07 March 2008 02:50 am, Johan Torp wrote:
> A typical signals n' slots
> use case is - at least for me:
>
> signal<void()> sig;
>
> class Foo{
> Foo() {
> con = sig.connect(bind(&Foo::some_func, this));
> }
>
> scoped_connection con;
>
> void some_func() {}
> };

To have the connection disconnect on the Foo object's destruction, you're
supposed to pass a shared_ptr owning the object (either directly or
indirectly) to slot::track() before connecting the slot. This insures the
object is not destroyed while a slot invocation is in progress (the signal
converts its weak_ptr copy to a shared_ptr while the slot runs), and
disconnects the slot when the tracked weak_ptr expires. It does have the
drawback that you often can't track connections made in the constructor
though, since enable_shared_from_this doesn't work there. I did provide
postconstructible/deconstruct_ptr to support postconstructors, although it
does all add up to a bit more typing.

I've attached an altered version of your example which does what I've
described.

-- 
Frank





Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk