Boost Signal References Object Out of Scope?

274 views Asked by At
typedef boost::signals2::signal<void ()> signal_t;

class AAA {

public:

void Connect(const signal_t::slot_type& subscriber)
{
    return m_sig.connect(subscriber);
}

void FireSignal()
{
    m_sig();
}   

private:

    signal_t sig;
};

// Global
AAA a;

BBB b;

// Some scope
{
...

a.Connect(boost::bind(&BBB:foo, &b));

...
}

Now the temporary object returned by previous boost::bind goes out of scope and gets destroyed However the temporary object is passed to AAA::Connect by reference. Now lets say at some point, object a.FireSignal() is called, does the signal calls a function object that's already destroyed??? How does it work otherwise???

1

There are 1 answers

2
Drax On BEST ANSWER

It makes a copy of it and keeps it.

The result of your to boost::bind is sued to construct a slot_type which stores that as a member and then once it is passed to signal::connect the signal stores the slot_type, which means a copy of your bind result is stored within the signal and that's the one used and called when the signal fires.

I can't find a place where this is specificaly and axplicitly said but you can more or less conclude that from: