views:

399

answers:

2

Im writing a XNA program that display pictures from a specific picture album on the Zune. The problem is when if I load the textures from all the pictures in the album, the program runs out of memory. I tried loading one by one and call Dispose on the previous picture texture once the user moves on to the next picture. This works but then I cannot get back the texture from the previous picture once it is disposed so the user cannot view back the past pictures without restarting the program!

+1  A: 

Don't store the texture handle per item in your album. Instead, use a single program-level handle that you dispose and load as needed as the user walks through the album.

GalacticCowboy
Yup that is what im doing. I only have one texture handle referencing one picture texture at any time. But once you access the texture from a Picture, the texture won't go away even when I assign a different texture to the handle. It seems to be cached somewhere. If I call Dispose on the texture, I cannot get back the texture from the Picture. It will keep returning null everytime I call GetTexture.
anonymous
Can you post some code? Are you sure it is a problem with the texture? I.e. is it maintaining a file lock that isn't getting cleared when you load the next file, and then prevents you from reloading the previous file?
GalacticCowboy
Thx for being so helpful! I have actually changed the code since where pictures now have to be attached to the XNA project and compiled together as XNB files. It works since I can now use ContentManagers to load and unload my pictures. However this is not a good solution since if I need to add, remove or change a picture, I have to recompile and deploy the whole program!! I dont have time now but in the weekend I will write a small program to re-create the problem and post the code.
anonymous
A: 

I would suggest doing all your data processing just once, and storing the filename (so you only have to do all your hierarchy/sorting once). Then only load a picture when you want it (exactly what the previous post suggested).

The problem with this method is that Content.Load(string) will load your texture, however if you lose all pointers to the texture the ContentManager will keep the texture in memory so that if you load it again it will load instantly. There is a method Content.Unload() which will drop these cached items, see:

A forum discussion on this topic:
http://forums.xna.com/forums/p/25978/141761.aspx
Shawn Hargreaves explains it:
http://blogs.msdn.com/shawnhar/archive/2006/09/06/743437.aspx

The way I would implement this is either to call unload every time you stop using a picture, or if you want faster loading (this depends on how often a user changes picture, if they're meant to be flicking through picture really quickly unloading every time is a bad idea) try catching out of memory exceptions and only calling unload then.

Martin
Thx for trying to help! Yes I will usually use a separate ContentManager for each texture so than I can unload it if I have to. However, the Zune Picture API does not use nor expose any ContentManager to allow me to control the loading and unloading. It only has one method called GetTexture. It does not use the normal Content.Load pattern! That is why I am so frustated! :-(
anonymous
Ahh, I didn't realise it was significantly different on the zune. Reading up on the MSDN a little (and this is a complete guess) could you not just call dispose on the Picture (not the texture, but the picture you called GetTexture), that should release all unmanaged resources being used by the picture (including its texture)
Martin