I am creating a windows game in xna using Visual C # Express. There are six SoundEffect objects in the game that regularly call Play () methods. The problem is that sometimes when the game closes, it crashes. This seems to happen when the window closes during audio playback. This is the message that appears in Visual C #:
AccessViolationException not handled
Attempted to read or write protected memory. This often indicates that another memory is corrupted.
There is no source in Visual Studio that can be debugged, and when the "get general help for this exception" button is clicked, a blank page will appear.
The code used is very similar to the MSDN example. This seems like a problem that exists somewhere in the base structure somewhere, and not on my code. But of course I donβt know for sure. This has happened many times.
http://msdn.microsoft.com/en-us/library/bb195053.aspx
Details below are for details:
System.AccessViolationException was unhandled Message=Attempted to read or write protected memory. This is often an indication that other memory is corrupt. Source=Microsoft.Xna.Framework StackTrace: at Microsoft.Xna.Framework.Audio.AudioCallbackDispatcher.IsEventRegistered(EventType type) at Microsoft.Xna.Framework.Audio.AudioCallbackDispatcher.UnregisterEvent(EventType type) at Microsoft.Xna.Framework.Audio.KernelMicrophone.ShutdownCaptureEngine() at Microsoft.Xna.Framework.Audio.MicrophoneUnsafeNativeMethods.ShutdownCaptureEngine() at Microsoft.Xna.Framework.Audio.AudioRendererShutdownHandler.AppExitingEventHandler(Object sender, EventArgs args) InnerException:
(I also have music playing through MediaPlayer, but I don't think this is connected.)
EDIT: It seems I found something that works, but it's kind of hacky and really should not be necessary. I am still open to more elegant solutions.
Call this line in Game1.UnloadContent (). He will be sure (if your sound effects are less than 3 seconds) that the sound does not play when the program really closes.
System.Threading.Thread.Sleep(3000);
source share