I recently realized Texture2D implements IDisposable, and now (while trying to track down a memory leak) wonder about where and when I should dipose this.

My current architecture is that I have a SpriteComponent instance which encapsulates and hides the Texture2D. Internally, when I switch screens (eg. MainMenuScreen => WorldMapScreen), I call Dispose on the current screen, which calls dispose on the entities, which calls dispose on the textures.

But this results in some wacked-out behaviour, like textures apprearing super zoomed in momentarily, or appearing black, or appearing as garbage image data.

So my question is really, when should I call Dispose on Texture2Ds?

(FYI, I also can switch to a screen, and then later switch back to it, which currently causes me to reload the sprites, and by proxy, the textures.)

  • 2
    \$\begingroup\$ Related: gamedev.stackexchange.com/questions/5615/… \$\endgroup\$ – ashes999 Jan 22 '13 at 14:13
  • \$\begingroup\$ The related link you just posted is pretty important. You don't say how you are loading these textures - the correct answer depends on how they are loaded! \$\endgroup\$ – Andrew Russell Jan 23 '13 at 7:09
  • \$\begingroup\$ @AndrewRussell I'm loading them with a standard content manager, which I use for all my content. \$\endgroup\$ – ashes999 Jan 23 '13 at 11:40
  • \$\begingroup\$ Well, in that case, as I'm sure you know from that link, you shouldn't Dispose them at all ;) You have to unload the whole ContentManager so it will also stop caching the disposed instances. \$\endgroup\$ – Andrew Russell Jan 24 '13 at 5:24

Well, the obvious answer, dispose of them only when you're completely done with them. If you know a screen will not be loaded again in a certain session of a game, it's safe to unload the textures used exclusively in that screen.

If all the textures or most of the textures are going to change, you may want to unload them to make room for the new textures. However, if you're not over your memory budget, there's no reason to unload textures that may be used again.

Edits from comments: For most games, unless you're loading a huge amount of textures, it's normal to load the textures up-front and keep them loaded for the entire play session. Disposing and re-loading is probably expensive, and it's simpler to just leave them in memory.

  • \$\begingroup\$ Do you mean that textures should live forever, and only be disposed if they won't be loaded again for that game? To clarify, I'm not talking about unloading them from the content manager, but invoking Dispose on them. \$\endgroup\$ – ashes999 Jan 22 '13 at 17:28
  • \$\begingroup\$ @ashes999 How much texture data have you got? Most indie games are small enough that loading everything when the game is launched and just keeping it in memory until termination works just fine. \$\endgroup\$ – aaaaaaaaaaaa Jan 22 '13 at 17:54
  • \$\begingroup\$ @ashes999 Yes, live for the entire life of your game, only disposed if they won't be loaded again in the life of that game. As eBusiness says, typically there's enough video memory to keep your textures loaded for the entire play session. \$\endgroup\$ – MichaelHouse Jan 22 '13 at 18:32
  • \$\begingroup\$ I have only a one-month-old project, so not much. Still, I load things as needed, not all up front. I guess this answers my question though. \$\endgroup\$ – ashes999 Jan 22 '13 at 19:46

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.