fix AudioDevice crash on shutdown

pull/50/head
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;
internal readonly object StateLock = new object();
private bool Running;
private bool IsDisposed;
public unsafe AudioDevice()
@ -130,13 +131,15 @@ namespace MoonWorks.Audio
Thread.IsBackground = true;
Thread.Start();
Running = true;
TickStopwatch.Start();
previousTickTime = 0;
}
private void ThreadMain()
{
while (!IsDisposed)
while (Running)
{
lock (StateLock)
{
@ -278,8 +281,9 @@ namespace MoonWorks.Audio
{
if (!IsDisposed)
{
lock (StateLock)
{
Running = false;
Thread.Join();
if (disposing)
{
foreach (var weakReference in resources)
@ -300,7 +304,6 @@ namespace MoonWorks.Audio
IsDisposed = true;
}
}
}
~AudioDevice()
{

View File

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

View File

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