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

Drax
  • 12,682
  • 7
  • 45
  • 85
Joseph
  • 3
  • 2

1 Answers1

0

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:

Drax
  • 12,682
  • 7
  • 45
  • 85
  • I tried both using pass by reference and object. The boost signals2 crashes! Somehow the signal loses the function object created by bind. – Joseph Jan 12 '17 at 02:03
  • @Joseph It is more likely that the instance you are using (`BBB b`) died than the bind result itself, additionally there could be other reasons, it really depends on the context. – Drax Jan 12 '17 at 09:23