MonoGame / MonoGame

One framework for creating powerful cross-platform games.
http://www.monogame.net
Other
11.3k stars 2.9k forks source link

SoundEffectInstancePool.Update() exception caused by multithread implimentations #6806

Open essex-pbrink opened 5 years ago

essex-pbrink commented 5 years ago

Microsoft.XNA.Frameworld.Audio.SoundEffectInstancePool.Update() crashes with an instance not found exception. This error in Visual Studio tags the Program.Main() Run() command.

SoundPool_Error

Appears to be a list insertion/loop error in C# when multithreading. The gameloop base.update() is calling audio management while threads are allowed to make additions to the play pool with SoundEffectInstance calls. Similar problem may occur due to multiple items being added to the pool simultaneously.

This is a common problem in multithreaded applications which modify a single list from multiple threads. C# has difficulty with supporting list changes as they are being iterated. Normally not an issue, but as games have to utilize threads more...it may be worth correcting the problem.

Workaround solution was to create a globally accessible object variable for use with lock(). Any new sound effect instances/.play() calls are contained in the lock on this variable as well as the gameloop.update(){ base.Update() } call.

Recommend adding an improved exception handling on this, or have a lock() within the framework to prevent list changes while performing the update loop through the list of SFX instances.

SoundPool_Fix

What version of MonoGame does the bug occur on:

What operating system are you using:

What MonoGame platform are you using:

Jjagg commented 5 years ago

Yeah, this should definitely be thread safe.