Add finaliser to WorkingBeatmap

This commit is contained in:
Dean Herbert 2019-06-27 13:56:36 +09:00
parent 1830362337
commit 8b0aaccfe6
3 changed files with 28 additions and 12 deletions

View File

@ -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();

View File

@ -46,6 +46,11 @@ namespace osu.Game.Beatmaps
skin = new RecyclableLazy<Skin>(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();
}
/// <summary>
/// Eagerly dispose of the audio track associated with this <see cref="WorkingBeatmap"/> (if any).
/// Accessing track again will load a fresh instance.
/// </summary>
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<T>
{
private Lazy<T> lazy;

View File

@ -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();