Monogame - Fixing the AccessViolationException

This is just a quick tutorial about how to fix the AccessViolationException that you're likely to run across eventually, as your program is closing (or other things).

This is a bug in MonoGame that it would appear has been fixed in the nightly builds, but has yet to be deployed into production. The day will soon come where the change I'm about to tell you to make won't be needed any more. (If you notice that that day has come, and you still see this tutorial in the list, please tell me so that I can fix it.)

Basically, this error is probably coming up because your game object isn't being disposed of correctly. The way to fix this is to go to your Program.cs file and change this code:

game = new Game1 ();
game.Run ()

So that it says:

using (Game1 game = new Game1 ()) 
{
    game.Run ();
}

You can also delete the static class variable that says:

private static Game1 game;

Interestingly, this is basically changing it so that it looks like the code in the original XNA template. (I guess somebody forgot to make the Game class disposable!)


troubleshoot64.png Having problems with this tutorial? Try the troubleshooting page!