fix AudioDevice crash on shutdown

remotes/1695061714407202320/main
cosmonaut 2023-07-28 15:02:20 -07:00
parent e73c7ede55
commit 81cd397013
3 changed files with 40 additions and 16 deletions

View File

@ -42,6 +42,7 @@ namespace MoonWorks.Audio
private AutoResetEvent WakeSignal; private AutoResetEvent WakeSignal;
internal readonly object StateLock = new object(); internal readonly object StateLock = new object();
private bool Running;
private bool IsDisposed; private bool IsDisposed;
public unsafe AudioDevice() public unsafe AudioDevice()
@ -130,13 +131,15 @@ namespace MoonWorks.Audio
Thread.IsBackground = true; Thread.IsBackground = true;
Thread.Start(); Thread.Start();
Running = true;
TickStopwatch.Start(); TickStopwatch.Start();
previousTickTime = 0; previousTickTime = 0;
} }
private void ThreadMain() private void ThreadMain()
{ {
while (!IsDisposed) while (Running)
{ {
lock (StateLock) lock (StateLock)
{ {
@ -278,27 +281,27 @@ namespace MoonWorks.Audio
{ {
if (!IsDisposed) if (!IsDisposed)
{ {
lock (StateLock) Running = false;
Thread.Join();
if (disposing)
{ {
if (disposing) foreach (var weakReference in resources)
{ {
foreach (var weakReference in resources) var target = weakReference.Target;
if (target != null)
{ {
var target = weakReference.Target; (target as IDisposable).Dispose();
if (target != null)
{
(target as IDisposable).Dispose();
}
} }
resources.Clear();
} }
resources.Clear();
FAudio.FAudioVoice_DestroyVoice(MasteringVoice);
FAudio.FAudio_Release(Handle);
IsDisposed = true;
} }
FAudio.FAudioVoice_DestroyVoice(MasteringVoice);
FAudio.FAudio_Release(Handle);
IsDisposed = true;
} }
} }

View File

@ -311,6 +311,18 @@ namespace MoonWorks.Audio
protected override unsafe void Destroy() protected override unsafe void Destroy()
{ {
foreach (var instance in UsedInstances)
{
instance.Free();
}
foreach (var instance in AvailableInstances)
{
instance.Dispose();
}
AvailableInstances.Clear();
if (OwnsBuffer) if (OwnsBuffer)
{ {
NativeMemory.Free((void*) Handle.pAudioData); NativeMemory.Free((void*) Handle.pAudioData);

View File

@ -86,12 +86,21 @@ namespace MoonWorks
Tick(); Tick();
} }
Logger.LogInfo("Starting shutdown sequence...");
Logger.LogInfo("Cleaning up game...");
Destroy(); Destroy();
Logger.LogInfo("Closing audio thread...");
AudioDevice.Dispose(); AudioDevice.Dispose();
Logger.LogInfo("Unclaiming window...");
GraphicsDevice.UnclaimWindow(MainWindow); GraphicsDevice.UnclaimWindow(MainWindow);
Logger.LogInfo("Disposing window...");
MainWindow.Dispose(); MainWindow.Dispose();
Logger.LogInfo("Disposing graphics device...");
GraphicsDevice.Dispose(); GraphicsDevice.Dispose();
SDL.SDL_Quit(); SDL.SDL_Quit();