Fix multiple cases of using objects which may be disposed

This commit is contained in:
Dean Herbert
2023-11-17 15:11:03 +09:00
parent 852fb723bc
commit a41fc78cf0
3 changed files with 24 additions and 9 deletions

View File

@@ -41,16 +41,22 @@ namespace osu.Framework.Benchmarks
[Benchmark]
public void BenchmarkRawCachingReuse()
{
using (var store = new RawCachingGlyphStore(baseResources, font_name) { CacheStorage = sharedTemp })
using (var store = new RawCachingGlyphStore(baseResources, font_name))
{
store.CacheStorage = sharedTemp;
runFor(store);
}
}
[Benchmark(Baseline = true)]
public void BenchmarkRawCaching()
{
using (var temp = new TemporaryNativeStorage("fontstore-test" + Guid.NewGuid()))
using (var store = new RawCachingGlyphStore(baseResources, font_name) { CacheStorage = temp })
using (var store = new RawCachingGlyphStore(baseResources, font_name))
{
store.CacheStorage = temp;
runFor(store);
}
}
[Benchmark]

View File

@@ -47,13 +47,17 @@ namespace osu.Framework.Tests.IO
[Test]
public void TestNoCrashOnMissingResources()
{
using (var glyphStore = new RawCachingGlyphStore(fontResourceStore, "DoesntExist") { CacheStorage = storage })
using (var fontStore = new FontStore(new DummyRenderer(), glyphStore, 100))
using (var glyphStore = new RawCachingGlyphStore(fontResourceStore, "DoesntExist"))
{
Assert.That(glyphStore.Get('a'), Is.Null);
glyphStore.CacheStorage = storage;
Assert.That(fontStore.Get("DoesntExist", 'a'), Is.Null);
Assert.That(fontStore.Get("OtherAttempt", 'a'), Is.Null);
using (var fontStore = new FontStore(new DummyRenderer(), glyphStore, 100))
{
Assert.That(glyphStore.Get('a'), Is.Null);
Assert.That(fontStore.Get("DoesntExist", 'a'), Is.Null);
Assert.That(fontStore.Get("OtherAttempt", 'a'), Is.Null);
}
}
}

View File

@@ -33,12 +33,17 @@ namespace osu.Framework.IO.Stores
return stream?.ReadAllBytesToArray();
}
public virtual Task<byte[]> GetAsync(string name, CancellationToken cancellationToken = default)
public virtual async Task<byte[]> GetAsync(string name, CancellationToken cancellationToken = default)
{
this.LogIfNonBackgroundThread(name);
using (Stream stream = storage.GetStream(name))
return stream?.ReadAllBytesToArrayAsync(cancellationToken);
{
if (stream == null)
return null;
return await stream.ReadAllBytesToArrayAsync(cancellationToken).ConfigureAwait(false);
}
}
public Stream GetStream(string name)