Release all texture memory

家住魔仙堡 提交于 2019-12-13 00:13:18

问题


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.


回答1:


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.




回答2:


Did you also try purgeSharedTextureCache as noted here:

http://www.cocos2d-iphone.org/api-ref/0.99.0/interface_c_c_texture_cache.html#ac673f75cd5418bdcecf1fb8717a40345

You can see it in work also in your App Delegate methods that release textures during memory warnings.



来源:https://stackoverflow.com/questions/9865356/release-all-texture-memory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!