I was trying to save some processing time by reusing textures. I would create a single sprite (not added to the scene), and then when I required a copy of it, create a new sprite using createWithTexture. I would then just pass the texture from the object that was never added to the scene.
Unfortunately it appears as if those objects get garbage collected for some reason? I don’t lose a reference to them since the static object is a member variable, so it’s not being deleted for that reason. Is there another explanation as to why this isn’t working correctly? I was able to fix the problem by adding the images to the scene and setting them to be invisible, but that seems like a strange solution to me and I’d like to understand why the objects are becoming invalid.
If you add the texture to the texture cache, it should stay in memory until until you either remove it from the cache or until a low memory warning. I would imagine if you retained the texture it wouldn’t be removed until you remove it manually.
Do you retain your sprite? Because if you use Sprite’s create function and don’t retain the sprite or add it to the scene, it will get released, freed from memory and the texture might be removed too. Not sure if the texture is removed immediately or added to the cache and removed if low memory warning though.
1 Like
Good calllll – I think the problem is in the “retain”. My assumption was that because I had a pointer to the sprite that the reference counting was working. But I think you’re quite right – I need to explicitly retain() it. That’ll probably do the trick, thanks!