0
votes

I am trying to re-program some old Arcade games in C# with the Monogame engine. However, I have encountered a certain problem.

Since my code tends to be a bit messed up, I am often not taking the effort to reset everything when the player successfully completes the game. Instead, I am simply closing the current Game-instance and opening a new one, like this: (in Program.cs)

    if (startgame)
    {
        do
        {
            using (var game = new Game1(level, points, soundOn))
                game.Run();
        } while (continueGame == true);
    }

Now the problem. In Game1, I am declaring and playing various SoundEffects. The first run everything works fine, but in all following Game1-instances, my program will always throw an System.AccessViolationException related to SharpDX.XAudio2.dll at the moment I am calling the .Play()-Method of a SoundEffect.

I tried playing SoundEffectInstances instead of the actual SoundEffects. Now it doesn't crash anymore, but is being completely silent from level 2 on instead.

Do you know what could be the reason of this error? Is my game-restarting loop causing problems I did not know about?

Thank you in advance.

(I am using Win 7 64 Bit, VS Express 2015 and Monogame 3.6)

2

2 Answers

0
votes

Do you know what could be the reason of this error? Is my game-restarting loop causing problems I did not know about?

Yes. Do not do it. You are destroying the whole game object just to reset the game. This is a very bad style, because it destroys the whole instance and everything that is connected to it. If you use static variables within it, they will remain in memory which may be the problem you have.

If you want to reset your game, simply write a Reset-method for everything and call these instead. Cleanup and nice code is something you should use instead of terrible hacks.

Can you show more code, so it is possible to understand, what went wrong?

PS: Never write something like while (continueGame == true) always go for while (continueGame).

0
votes

There is a chance that the issue is due to SharpDX's reaction to the media file itself. For instance, an MP3 file that would work perfectly fine under XNA 4.0 loaded as a SoundEffect (with associated SoundEffectInstance) may crash the game under MonoGame UWP. (This happened to me with one file, but I was already using SoundEffectInstance in the first place when the crashing started.)

As such, have you tried re-encoding your relevant audio files, such as through a utility like Audacity? Although it may not solve the internal issue per se (some element of the audio file breaking SharpDX), if a re-encoded file doesn't have the problem element in the first place, that should be a valid solution for you.