MoonWorks/src/Audio/AudioResource.cs

54 lines
1.2 KiB
C#
Raw Normal View History

2022-02-23 05:14:32 +00:00
using System;
using System.Runtime.InteropServices;
2021-02-27 00:17:26 +00:00
namespace MoonWorks.Audio
{
2022-02-23 05:14:32 +00:00
public abstract class AudioResource : IDisposable
{
public AudioDevice Device { get; }
public bool IsDisposed { get; private set; }
private GCHandle SelfReference;
2022-02-23 05:14:32 +00:00
protected AudioResource(AudioDevice device)
2022-02-23 05:14:32 +00:00
{
Device = device;
SelfReference = GCHandle.Alloc(this, GCHandleType.Weak);
Device.AddResourceReference(SelfReference);
2022-02-23 05:14:32 +00:00
}
2023-12-09 00:33:52 +00:00
protected virtual void Dispose(bool disposing)
2022-02-23 05:14:32 +00:00
{
if (!IsDisposed)
{
if (disposing)
{
Device.RemoveResourceReference(SelfReference);
SelfReference.Free();
}
2022-02-23 05:14:32 +00:00
IsDisposed = true;
}
}
~AudioResource()
{
#if DEBUG
2023-12-09 00:33:52 +00:00
// If you see this log message, you leaked an audio resource without disposing it!
// We can't clean it up for you because this can cause catastrophic issues.
// You should really fix this when it happens.
Logger.LogWarn($"A resource of type {GetType().Name} was not Disposed.");
#endif
2022-02-23 05:14:32 +00:00
}
public void Dispose()
{
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
}
2021-02-27 00:17:26 +00:00
}