Created
August 7, 2012 03:54
-
-
Save jeffomatic/3281352 to your computer and use it in GitHub Desktop.
blog: jl_signal, code: iterating through signal connections
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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