From 8b0aaccfe618dd9fd92e43718458e7315594ee40 Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Thu, 27 Jun 2019 13:56:36 +0900 Subject: [PATCH] Add finaliser to WorkingBeatmap --- osu.Game.Tests/WaveformTestBeatmap.cs | 2 +- osu.Game/Beatmaps/WorkingBeatmap.cs | 36 +++++++++++++++++++-------- osu.Game/Tests/Visual/OsuTestScene.cs | 2 +- 3 files changed, 28 insertions(+), 12 deletions(-) diff --git a/osu.Game.Tests/WaveformTestBeatmap.cs b/osu.Game.Tests/WaveformTestBeatmap.cs index fdb91b7c5b..c5a2f5be51 100644 --- a/osu.Game.Tests/WaveformTestBeatmap.cs +++ b/osu.Game.Tests/WaveformTestBeatmap.cs @@ -30,7 +30,7 @@ namespace osu.Game.Tests trackStore = audioManager.GetTrackStore(reader); } - public override void Dispose() + protected override void Dispose(bool isDisposing) { base.Dispose(); stream?.Dispose(); diff --git a/osu.Game/Beatmaps/WorkingBeatmap.cs b/osu.Game/Beatmaps/WorkingBeatmap.cs index a1864526d1..61390fe51b 100644 --- a/osu.Game/Beatmaps/WorkingBeatmap.cs +++ b/osu.Game/Beatmaps/WorkingBeatmap.cs @@ -46,6 +46,11 @@ namespace osu.Game.Beatmaps skin = new RecyclableLazy(GetSkin); } + ~WorkingBeatmap() + { + Dispose(false); + } + protected virtual Track GetVirtualTrack() { const double excess_length = 1000; @@ -199,22 +204,33 @@ namespace osu.Game.Beatmaps other.track = track; } - public virtual void Dispose() - { - background.Recycle(); - waveform.Recycle(); - storyboard.Recycle(); - skin.Recycle(); - - beatmapCancellation.Cancel(); - } - /// /// Eagerly dispose of the audio track associated with this (if any). /// Accessing track again will load a fresh instance. /// public virtual void RecycleTrack() => track.Recycle(); + #region Disposal + + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } + + protected virtual void Dispose(bool isDisposing) + { + // recycling logic is not here for the time being, as components which use + // retrieved objects from WorkingBeatmap may not hold a reference to the WorkingBeatmap itself. + // this should be fine as each retrieved comopnent do have their own finalizers. + + // cancelling the beatmap load is safe for now since the retrieval is a synchronous + // operation. if we add an async retrieval method this may need to be reconsidered. + beatmapCancellation.Cancel(); + } + + #endregion + public class RecyclableLazy { private Lazy lazy; diff --git a/osu.Game/Tests/Visual/OsuTestScene.cs b/osu.Game/Tests/Visual/OsuTestScene.cs index c8798448ae..9f4532513f 100644 --- a/osu.Game/Tests/Visual/OsuTestScene.cs +++ b/osu.Game/Tests/Visual/OsuTestScene.cs @@ -137,7 +137,7 @@ namespace osu.Game.Tests.Visual track = audio?.Tracks.GetVirtual(length); } - public override void Dispose() + protected override void Dispose(bool isDisposing) { base.Dispose(); store?.Dispose();