Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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.)

share|improve this question
2  
Related: gamedev.stackexchange.com/questions/5615/… –  ashes999 Jan 22 '13 at 14:13
    
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! –  Andrew Russell Jan 23 '13 at 7:09
    
@AndrewRussell I'm loading them with a standard content manager, which I use for all my content. –  ashes999 Jan 23 '13 at 11:40
    
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. –  Andrew Russell Jan 24 '13 at 5:24
add comment

1 Answer

up vote 2 down vote accepted

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.

share|improve this answer
    
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. –  ashes999 Jan 22 '13 at 17:28
    
@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. –  eBusiness Jan 22 '13 at 17:54
    
@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. –  Byte56 Jan 22 '13 at 18:32
    
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. –  ashes999 Jan 22 '13 at 19:46
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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