From a3c602407f3b3b5d318babd635f28a3ce65ce442 Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Fri, 16 Sep 2011 22:57:25 +1000 Subject: [PATCH] Store loop conditions before dispatching signal If we store the loop condition before executing the functor we have a better chance of avoiding iterator invalidation from any trickery on the subscriber side. --- signal.hpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/signal.hpp b/signal.hpp index 594bd63f..ef40fa9f 100644 --- a/signal.hpp +++ b/signal.hpp @@ -106,9 +106,14 @@ namespace util { /// Execute all callbacks, ignoring the return parameters. Does not combine results. void operator () (Args... tail) { - for (auto i = m_children.cbegin (); i != m_children.cend (); ) { + auto i = m_children.cbegin (); + bool looping = m_children.cend () != i; + + while (looping) { // Increment before we execute so that the caller is able to deregister during execution. auto current = i++; + looping = m_children.cend () != i; + (*current)(tail...); } }