make pd_unbind safe (+ improve stack overflow protection) #849
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
a) make
pd_unbindsafeCurrently, it is unsafe to dynamically unbind symbols because some other object could be sending to them.
The solution is to simply make a copy of the bind list before iterating over it. A counter keeps track of the total number of items. We use stack allocation up to a certain limit and then switch to heap allocation to avoid a stack overflow.
To make copying as fast as possible, I changed the bind list implementation to a dynamically sized array instead of a linked list. (This is also better for cache coherency).
In a way, this not much different than how
[list]has to output a copy of the stored list to prevent other objects to modify the list while iterating over it.This PR would finally make it possible to safely implement dyamically settable
[receive]objects (#604), and make existing similar objects safe, for example iemgui's[receive(method.b) improve stack overflow protection
The current overflow protection only works for a single recursion path. For more than 1 path (e.g. several outlets feeding back to the same inlet), Pd would end up in an infinite loop. Once the stacklimit is reached, I set an overflow flag to prevent any further messaging until the stack got completely unwound.