Monogame crashes through SharpDX on XBOXONE/PC while playing multiple SoundEffects in parallel

Hi all,

recently have encountered an issue while playing multiple SoundEffectInstances in parallel. Tried to search but without success, all i found were recommendations to play in parallel through Tasks, try to stay around 4 active instances, dispose each instance after playing. Have to say the stability is better while trying to do so but game still crashes. Believe doing something wrong or is it a known bug in the source of SharpDX? Was not able to find a “known bug”, but noticed there is plenty of other issues with “SharpDX.XAudio2”, while was trying to find a solution.

Got to this exception:

  Message=Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
  Source=<Cannot evaluate the exception source>
<Cannot evaluate the exception stack trace>
System.NullReferenceException: Object reference not set to an instance of an object.
   at SharpDX.XAudio2.SourceVoice.GetState(Int32 flags)
   at Microsoft.Xna.Framework.Audio.SoundEffectInstance.PlatformGetState()
   at Microsoft.Xna.Framework.Audio.SoundEffectInstancePool.Update()
   at Microsoft.Xna.Framework.FrameworkDispatcher.Update()
   at Microsoft.Xna.Framework.Game.DoUpdate(GameTime gameTime)
   at Microsoft.Xna.Framework.Game.Tick()
   at Microsoft.Xna.Framework.UAPGamePlatform.<>c.<Start>

Primarily targeting XBOXONE but testing on PC as well, its and UWP app.

What is the best and safest way to use multiple Sound Effects at once please?



You should not get SharpDX exceptions, so that’s a bug in the runtime that we’d like to address. Can you share your code to play sound effects or a project that reproduces the issue?

I recommend doing everything from the main thread. I’m not sure how thread-safe our implementation is and under the hood the actual playback happens in a dedicated thread anyway. SoundEffect represents the audio data for a sound and you should never have more than 1 instance for a certain sound. When playing a sound it gets turned into a SoundEffectInstance. Internally instances for a single sound effect reference the same audio data.

To play a SoundEffect there’s two API’s you can use. If you just want to play a sound effect start to finish without changing playback parameters such as volume and pitch, you can use SoundEffect.Play. That creates an instance and plays it for you. If you need more control you can create a SoundEffectInstance. With that you can control playback and parameters.

If you’re looking for documentation but can’t find it for MonoGame, try searching for XNA documentation. There’s a bunch of learning resources and the API is almost exactly the same (MonoGame is a reimplementation of the XNA API).

1 Like

Thanks for response, I’ll share the code once get back to it over weekend (possibly earlier if time allows).

1 Like

Sharing source code

Tried to do some more research but without success, tried also different wav quality (44Khz, 8bit - 44Khz, 32 bit) but did not help.

1 Like