MyGUI / mygui

Fast, flexible and simple GUI.
http://mygui.info/
Other
713 stars 205 forks source link

Fix iterator invalidation due to delegate modification during invocation #260

Closed Assumeru closed 1 year ago

Assumeru commented 1 year ago

Fixes https://github.com/MyGUI/mygui/pull/256#issuecomment-1583264606

Prior to #256, mListDelegates iterators could only be invalidated by calls to operator() as all the other methods didn't remove list element. A recursive call to operator() could have still caused iterator invalidation.

This PR should address both issues by only removing list elements in operator() (and clear) and only while no other is doing the same. (It's still not thread safe, of course.)

I can't say I like the pattern of removing delegates from inside a delegate, but disallowing it is probably not an option. Likewise, switching to a vector and using an index based loop to prevent iterator invalidation while still allowing methods to remove delegates is probably not a good solution as it means delegates are no longer guaranteed to be invoked.