Fix potential hierarchy mutation from async context

This commit is contained in:
smoogipoo 2020-08-12 01:33:06 +09:00
parent eec94e1f53
commit 688e447950
1 changed files with 21 additions and 4 deletions

View File

@ -317,11 +317,28 @@ private void beatmapChanged(ValueChangedEvent<WorkingBeatmap> beatmap)
private void changeTrack()
{
CurrentTrack.Expire();
CurrentTrack = new DrawableTrack(current.GetTrack());
CurrentTrack.Completed += () => onTrackCompleted(current);
var lastTrack = CurrentTrack;
AddInternal(CurrentTrack);
var newTrack = new DrawableTrack(current.GetTrack());
newTrack.Completed += () => onTrackCompleted(current);
CurrentTrack = newTrack;
// At this point we may potentially be in an async context from tests. This is extremely dangerous but we have to make do for now.
// CurrentTrack is immediately updated above for situations where a immediate knowledge about the new track is required,
// but the mutation of the hierarchy is scheduled to avoid exceptions.
Schedule(() =>
{
lastTrack.Expire();
if (newTrack == CurrentTrack)
AddInternal(newTrack);
else
{
// If the track has changed via changeTrack() being called multiple times in a single update, force disposal on the old track.
newTrack.Dispose();
}
});
}
private void onTrackCompleted(WorkingBeatmap workingBeatmap)