Free SKTextureAtlas from memory

I have a problem getting a texture atlas. I currently have a SpriteKit game where a player can change his character. Right now I have an atlas in a global shared instance, for example.

let char0Atlas: SKTextureAtlas = SKTextureAtlas(named: "Atlas/iPhone/char0") let char1Atlas: SKTextureAtlas = SKTextureAtlas(named: "Atlas/iPhone/char1") let char2Atlas: SKTextureAtlas = SKTextureAtlas(named: "Atlas/iPhone/char2") 

Each character you can change has its own atlas. I get the player moving, idle and transition animation from the atlas of characters using this method:

 func charAnimation(char: CharEnum) -> [SKTexture] { var temp: [SKTexture] = [] var name: String = "" let atlas: SKTextureAtlas = char.atlasForChar() for i in 1...20 { if i > 9 { name = "char_\(charId)_idle_00\(i)" } else { name = "char_\(charId)_idle_000\(i)" } temp.append(atlas.textureNamed(name)) } return temp } 

And this is stored in the array instance variable in the node sprite class. Thus, every time a character changes, these frames are replaced with new frames, so should the old ones be freed correctly?

 class PlayerNode: SpriteNode { private var currentAnimation: [SKTexture] = [] private var animations: (idle: [SKTexture], run: [SKTexture], jump: [SKTexture]) = PlayerController.animationsForHero(.CharOne) } 

In addition, when the player switches the characters, I use this to preload the texture atlas:

 SKTextureAtlas.preloadTextureAtlases([char.atlasForHero()], withCompletionHandler: { () -> Void in updateChar() }) 

Why does SpriteKit never free memory from previous character animations? If the player switches to new characters, the memory is constantly increasing and the application crashes. If a character that has already been selected in this session is selected again, the memory does not increase. This indicates a memory leak. Character animations are not freed. Why?

I understand that SpriteKit must take care of this myself, so this is confusing. Is it impossible to manually delete the texture atlas on my own?

Thanks!

+4
source share
1 answer

@dragoneye first point is correct: you must pre-load SKTextureAtlas only once.

Take a look at the Apple documentation for working with sprites :

-

  • Preloading textures into memory
  • Removing texture from memory

Then, if the memory is not freed, this may be due to the fact that SKTexture references still exist, as indicated by the second dot:

After the texture is loaded into the graphic memory, it will remain in memory until the SKTexture binding object is SKTexture . This means that between levels (or in a dynamic game), you may need to remove the texture object. Delete the SKTexture object object by removing any strong references to it, including:

  • All texture references from SKSpriteNode and SKEffectNode objects in your game
  • Any strong texture references in your own code
  • The SKTextureAtlas object that was used to create the texture object
+2
source

All Articles