Skip to content

Instantly share code, notes, and snippets.

@jeffomatic
Created August 7, 2012 03:54
Show Gist options
  • Save jeffomatic/3281352 to your computer and use it in GitHub Desktop.
Save jeffomatic/3281352 to your computer and use it in GitHub Desktop.
blog: jl_signal, code: iterating through signal connections
// A sample Emit() method on a signal object for slots that take
// two arguments.
void Emit( TArg1 a1, TArg2 a2 ) const
{
// Here, the iterator is just a pointer to a list node.
// Incrementing the iterator means that we just follow the node's
// "next" pointer. But what happens if the current node is freed
// before the iterator can be incremented?
for ( ConnectionListIter i = m_oConnections.begin();
i.isValid();
++i )
{
i->delegate.invoke( a1, a2 );
}
// I also considered the following design, which solves the
// above problem by moving it to a different location.
// What if, by invoking the delegate, we disconnect the NEXT
// delegate? We're still left with a corrupt iterator.
ConnectionListIter i = m_oConnections.begin();
while ( i.isValid() )
{
ConnectionNode* c = *i;
++i;
c->delegate.invoke( a1, a2 );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment