[Boost-users] Connecting signals2::signal to another signals2::signal: trackable?

1,280 views
Skip to first unread message

Chris Stankevitz

unread,
Aug 2, 2012, 9:35:23 PM8/2/12
to Boost...@lists.boost.org
class A { boost::signals2::signal<void()> Signal; }
class B { boost::signals2::signal<void()> Signal; }

boost::shared_ptr<A> pA = boost::make_shared<A>();
boost::shared_ptr<B> pB = boost::make_shared<B>();

===

Q1: Is it possible to connect A's signal to B?

A1: Yes:

void f()
{
pA->Signal.connect(pB->Signal);
pA->Signal();
}

===

Q2: Will I get a crash if pB is subsequently released?

A2: Yes

void f()
{
pA->Signal.connect(pB->Signal);
pB.reset();
pA->Signal();
// crash!
}

===

Q3: Is it possible to use signals2::signal::slot_type::track to avoid the crash?

A3: [I do not know the answer, please help me here]

===

Thank you,

Chris
_______________________________________________
Boost-users mailing list
Boost...@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users

Frank Mori Hess

unread,
Aug 2, 2012, 10:29:59 PM8/2/12
to boost...@lists.boost.org
On Thursday, August 02, 2012, Chris Stankevitz wrote:
> Q2: Will I get a crash if pB is subsequently released?
>
> A2: Yes

It doesn't crash for me (I tried with the attached test program). Can you
provide a test program that produces the crash?

signals2_crash.cpp
signature.asc

Chris Stankevitz

unread,
Aug 3, 2012, 10:55:04 AM8/3/12
to boost...@lists.boost.org
On Thursday, August 2, 2012, Frank Mori Hess wrote:
It doesn't crash for me (I tried with the attached test program).  Can you
provide a test program that produces the crash?

Frank,

Thank you for your reply.  I did not run a test program; however, I do not understand how it could possibly be defined behavior.  By what mechanism could the signal know that its slot has been destroyed and should not be used?

I'll get back to you with a test program.

Thanks,

Chris

Chris Stankevitz

unread,
Aug 3, 2012, 11:03:50 AM8/3/12
to boost...@lists.boost.org
On Thu, Aug 2, 2012 at 7:29 PM, Frank Mori Hess <fmh...@speakeasy.net> wrote:
> It doesn't crash for me (I tried with the attached test program). Can you
> provide a test program that produces the crash?

Frank,

I updated your test program to by using signal::num_slots to test
whether your no-crash was a fluke or whether it was really safe. It
appears to be genuinely safe, although I do not understand how. If
anyone knows, please chime in!

Thank you,

Chris
test.cpp

Igor R

unread,
Aug 3, 2012, 1:06:53 PM8/3/12
to boost...@lists.boost.org
Excuse my ignorance, but what does connecting one signal to another
mean? Is it chaining the signals? Where is it documented?

Chris Stankevitz

unread,
Aug 3, 2012, 2:34:03 PM8/3/12
to boost...@lists.boost.org
On Fri, Aug 3, 2012 at 10:06 AM, Igor R <boost...@gmail.com> wrote:
> Excuse my ignorance, but what does connecting one signal to another
> mean? Is it chaining the signals? Where is it documented?

Igor,

You are correct, it is not documented. I assumed that it chains the
signals. Using GDB, this is what happens (not that I understand any
of it):

1. SignalA.connect(SignalB);

2. SignalB is passed as argument "f" to this function:

template<typename F>
BOOST_SIGNALS2_SLOT_CLASS_NAME(BOOST_SIGNALS2_NUM_ARGS)(const F& f)
{
init_slot_function(f);
}

3. Then SignalB is passed to:
template<typename F>
void init_slot_function(const F& f)
{
_slot_function = detail::get_invocable_slot(f, detail::tag_type(f));
signals2::detail::tracked_objects_visitor visitor(this);
boost::visit_each(visitor, f);
}

===

It appears that SignalB is converted to a slot. I don't know what
tracked_objects_visitor is, but perhaps it is the magic that keeps the
test program from crashing.

Chris
Reply all
Reply to author
Forward
0 new messages