Fix array shifting bug when stopping tweens inside callbacks#16
Open
nanodesuologist wants to merge 2 commits into
Open
Fix array shifting bug when stopping tweens inside callbacks#16nanodesuologist wants to merge 2 commits into
nanodesuologist wants to merge 2 commits into
Conversation
Author
|
Optimized also. The implemented fix uses a fast-path/slow-path approach. It verifies if the tween is still at index i. If it is (the 99.9% case), it uses the fast O(1) index removal. If the array shifted during execution, it safely falls back to the O(N) reference removal. |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
Calling tween:stop() from inside a callback (like onstart) can cause crashes or duplicate tween executions.
flux:remove uses a swap-and-pop optimization for array deletion. If the currently processing tween happens to be at the end of the array, stopping an older tween will teleport the current tween to a different index to fill the gap. However, the loop in flux:update still attempts to remove the current tween using its old array index (i), removing the wrong tween and leaving the current one in the array to execute a second time.
Solution
In flux:update, change flux.remove(self, i) to flux.remove(self, t). This ensures the currently finishing tween is deleted by reference, regardless of whether its index shifted during the frame's execution.
Minimal Reproducible Example
Using the original flux.lua, this script crashes. With the patch, it executes successfully.