Saturn
Saturn

Reputation: 18149

Properly disposing textures from tiled maps or sprites

I am unsure about how to completely kill a node that uses graphics in cocos2d-iphone.

So what exactly do I mean? Well, for instance, I have a CCTMXTiledMap. It uses some textures, right? Well, I no longer need to use this tiled map, so I kill it, and I assume that such textures a killed as well, and memory is freed.

I am using [[CCTextureCache sharedTextureCache] dumpCachedTextureInfo]; to get info about the textures currently held in memory.

    CCTMXTiledMap *map = [CCTMXTiledMap tiledMapWithTMXFile:@"myMap.tmx"];
    [self addChild:map z:0];
    [[CCTextureCache sharedTextureCache]
                             dumpCachedTextureInfo]; // I get 32MB usage.
    [self removeChild:map cleanup:YES]; // I assume this will "kill" the map.
    [[CCTextureCache sharedTextureCache]
                             dumpCachedTextureInfo]; // I STILL get 32MB usage

As you can see, removing the map from child doesn't seem to truly kill the texture being used by it.

How exactly am I supposed to free memory then? I think that the same happens with CCSprites etc. Acoording to CCTextureCache, I just keep stacking memory until my app crashes.

Note: I know I could use something like [[CCTextureCache sharedTextureCache] removeAllTextures]; and clean everything up. But that doesn't seem very efficient.

Upvotes: 0

Views: 305

Answers (1)

YvesLeBorg
YvesLeBorg

Reputation: 9079

CCTextureCache will only remove textures for which there exist NO references. I have not used the CTMXTileMap class myself, but maybe there are objects in there that reference the texture. The same happens with CCSpriteBatchNode. You must first remove the spriteFrames, then removeUnusedTextures from the cache, at which point the memory is reclaimed. Suggest you use removeUnusedTextures after removing the map as child.

I dont see an explicit cleanup in CTMXTileMap, it is relying on dealloc to remove its objects that could reference the texture. Maybe (not certain about this), you should give a chance to the autorelease pool to release finally the map, before taking a measurement again.

Upvotes: 1

Related Questions