cocos2d-objc icon indicating copy to clipboard operation
cocos2d-objc copied to clipboard

Calling CCSpriteFrameCache#removeUnusedSpriteFrames can be hazardous to your apps health

Open widgetrevolt opened this issue 11 years ago • 2 comments

I assume you already are aware of this because I see the general comments about needing to refractor the sprite framecache.

The bug is that if you call removeUnusedSpriteFrames it will remove the sprite frames but not update the _loadedFilenames cache in the CCSpriteFrameCache. Then when you go to reload one of those frames, it thinks its already loaded and will eventually return a nil sprite frame.

If you have two scenes (A & B) using two different sprite sheets and then you do the following, you will get crashes and errors in CCAnimationManager because SpriteFrames don't exist, for example:

  1. Load scene A using sprite sheet A
  2. navigate to scene B using sprite sheet B
  3. ! Call removeUnusedSpriteFrames. Sprites from sprite sheet A are unloaded
  4. navigate to scene A. The call to spriteFrameByName will call addSpriteFramesWithFile which will not reload the sprite sheet A because the file is still in the _loadedFilenames cache.

widgetrevolt avatar Jun 15 '14 20:06 widgetrevolt

I didn't know about that one, but that's not the only bug like it. :-\ I'm not really certain how to fix many of them without changing (breaking) the API. This one sounds pretty fixable though.

slembcke avatar Jun 16 '14 16:06 slembcke

Hmm. I suppose you could try to fix it - or maybe its just not used that often. I found the culprit in some 2+ year old library code so perhaps it would be easy to just deprecate that method or make it private and see how often others are using it.

widgetrevolt avatar Jun 16 '14 16:06 widgetrevolt