3
\$\begingroup\$

I have a problem with my game. I am using multiple classes for different screens, for exemple: Main Menu is one screen, When playing is another, and so on...

I decided to use multiple ContentManagers, different content for each screen, so the game won't consume very much RAM, and to load the screen's content when the user will navigate on that screen, and unload/dispose the content when user navigates away.

I have created different content managers for each screen that I had to, but for exemple if I navigate from main menu, to the playing screen, then back to the main menu (when the content from playing screen gets disposed), and then back to the playing screen, I get this error:

An exception of type 'System.ObjectDisposedException' occurred in MonoGame.Framework.DLL but was not handled in user code


Additional information: Cannot access a disposed object.

I googled this problem, and the only answer that I found was not to dispose the Content Manager, but each texture on that screen.

Is this the only way that I can handle this? To dispose each texture from the screen, when user navigates away?

Thanks.

\$\endgroup\$
  • 1
    \$\begingroup\$ ...so the game won't consume very much RAM... Are you really sure this is a problem that needs to be solved? \$\endgroup\$ – Steve H Jul 27 '15 at 0:49
1
\$\begingroup\$

Instead of disposing the contentmanager entirely; do ContentManager.Unload() This clears the contentmanager's content, freeing up resources and leaving it ready to load new content when it suits you.

See: https://msdn.microsoft.com/en-us/library/microsoft.xna.framework.content.contentmanager.unload.aspx

I use this method myself in a game where the player can choose from a lot of different characters. The contentmanager loads the required spritesheets and sounds; the rest of the code always points to the same texture variable in the contentmanager. When the game is over, that contentmanager is unloaded, only to load a new set of content when a new gameround starts right after the character selection screen.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.