UnloadsContentManagerWhenDestroyed

Introduction

UnloadsContentManagerWhenDestroyed is a property which controls whether a given Screen unloads its ContentManager when the Screen is destroyed. Most Screens should have this value set to ‘true’ because Screen transitions usually represent when content can be safely unloaded to free up RAM for new content. However, there are a few situations when this should be false. These are:

  • If the Screen is going to be visited frequently and the cost of holding content in RAM is offset by the savings in load time
  • If the Screen is a popup Screen, since popups usually use the same ContentManager as their containing Screens.

Popups and UnloadsContentManagerWhenDestroyed

Popups can by default cause a lot of problems with content. Let’s consider the following situation:

In this example we’ll use two Screens: MainScreen and PopupScreen. By default both Screens will use the same ContentManager. If using Glue, the ContentManager would be named “MainScreen”. First, MainScreen loads a texture to use as a background. Next it loads PopupScreen, which in turn loads a texture that is used as a font. Then later the Popup is destroyed, but the MainScreen is not. When the PopupScreen is destroyed, it will unload its ContentManager. Since the Popup uses the same ContentManager as MainScreen, then it unloads the “MainScreen” content manager. This means that simply creating and destroying the popup will result in the MainScreen’s background texture being unloaded as well. This will either cause a crash or may cause the Sprite to render a solid color (such as white).

The solution is fairly simple – set the PopupScreen’s UnloadsContentManagerWhenDestroyed to be false. Once this is done, the Popup can be destroyed without the MainScreen’s ContentManager being affected. Then when the MainScreen is destroyed it will unload the ContentManager that contains all of the content in the MainScreen as well as all of the content in PopupScreen.

If your PopupScreen is always going to be used as a popup, then you may want to set UnloadsContentManagerWhenDestroyed to false in the PopupScreen’s CustomInitialize. If the PopupScreen may be used as a popup in some cases but not in others, you will want to create a property that gives the MainMenu (and any other Screen using an instance of PopupScreen) access to the UnloadsContentManagerWhenDestroyed property.