Reputation: 18159
I load my animated character with a huge texture this way:
[[CCSpriteFrameCache sharedSpriteFrameCache]addSpriteFramesWithFile:@"MyFile.plist"];
And later, when I am no longer using the character, I release memory using:
[[CCSpriteFrameCache sharedSpriteFrameCache]removeSpriteFramesFromFile:@"MyFile.plist"];
And also:
[[CCTextureCache sharedTextureCache]removeTextureForKey:@"MyFile.png"];
Just to be sure.
I do that because the texture it uses consumes a lot of memory, so I make sure I dispose of it so my app doesn't crash. Is the above code correct?
Anyway, here is the actual problem: I eventually load my character again, and dispose of it again. Eventually, after a couple revives and kills, the app crashes. The log says nothing about it, so I can just suspect that it was due to memory - that's why I assumed that I wasn't disposing the texture properly.
I do need to create and dispose the character's texture. I can't afford to have it preloaded in the game.
Upvotes: 1
Views: 2001
Reputation: 9089
I usually use the brute force approach as follows:
[[CCSpriteFrameCache sharedSpriteFrameCache] removeUnusedSpriteFrames];
[[CCTextureCache sharedTextureCache] removeUnusedTextures];
at specific moments in the game flow. It is not too too costly to have coco scan its arrays, in the grand scheme of things. As in your case, in my current game i must load and ditch. CCTextureCache has CCLOG'ing that permit you to witness whether the removal actually occurs when you expect it, or close to it (autorelease has its quirks). If not, you are probably retaining some handle to the texture somewhere, a sprite, batch node, an CCAnimation, or an action.
Upvotes: 4
Reputation: 24771
Did you also try purgeSharedTextureCache
as noted here:
You can see it in work also in your App Delegate methods that release textures during memory warnings.
Upvotes: 0