• Advertisement
Sign in to follow this  

Instruments - can't find leak - Cocos2d

This topic is 1802 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts


This is an issue that is bothering me for a while. I also posted this on cocos2d forums but so far no help and decided to ask for help here as well.

So basically I have a MainMenuScene, which in turns creates a test layer. This layer is very simple as it only creates a score label and a button that is added to a menu. When this button is pressed the MainMenuScene is replaced by a blank scene and then the MainMenuScene is loaded again.

So far so good. However the problem is when I take a heapshot in the instruments each time the scene is reloaded, I get a heap increase of around 20kb. When I dig deeper in the extended view it seems the sprite and font from the testLayer aren't being released and yet the dealloc method from the layer is being called.

I also add ARC support to my files except core Cocos files. Could this be related in any way?

Here is the test layer:


@implementation TestLayer

self = [super initWithColor:ccc4(2,2,2,210)];
if (self != nil)
// ask director the the window size
CGSize s = [[CCDirector sharedDirector] winSize];

self.isTouchEnabled = YES;

NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
formatter.numberStyle = NSNumberFormatterDecimalStyle;

NSNumber * n = [NSNumber numberWithInt:[SDCloudUserDefaults integerForKey:@"highscore"]];

NSString *string = [formatter stringFromNumber:n];

CCLabelBMFont * scoreLabel = [CCLabelBMFont labelWithString:string fntFile:@"scoreFont.fnt" width:s.width alignment:kCCTextAlignmentCenter];
scoreLabel.position = ccp( POS_X(443), POS_Y(877) );
[self addChild:scoreLabel z:90];

CCMenuItemSprite *playButton = [CCMenuItemSprite itemFromNormalSprite:[CCSprite spriteWithFile:@"play_up.png"]
selectedSprite:[CCSprite spriteWithFile:@"play_down.png"]

playButton.position = ccp( POS_X(380) , POS_Y(177));

menu = [CCMenu menuWithItems: playButton, nil];
menu.position = ccp(0, 0);
[self addChild: menu];

return self;

-(void) Transition:(ccTime) dt
[self unschedule:@selector(Transition:)];
[[SceneManager sharedSceneManager]RunSceneWithID:SCENE_LOADING:eCCTransitionCrossFade:1.0f];


- (void) dealloc


So it seems to me that even though the dealloc methods are being called, the assets inside the layer aren't being released...I don't understand what is going on.
Any help would be much appreciated.

Share this post

Link to post
Share on other sites
A follow up to why you need to do this in case you, or anyone doesn't know.

It's because when you call the method addChild, it's added into a private array
which isn't being released, as you are overriding the CCLayer's dealloc method which is where that array is being released.

Share this post

Link to post
Share on other sites

Hi arcademissile and thanks for your input.


I did the same test on an unARCed project with all the correct calls to [super dealloc] etc, and got the exact same behavior.


However, after closer inspection, it seems I don't have leaks after all but was just not interpreting the results correctly.


So I have the program on an initial state and take a heapshot. I then press the ui button that makes the scene to be replaced and then reloaded. I get back to the initial state and take another heapshot and notice that the heap had a growth of around 20kb or so. When i press the ui button again the previous heapshot goes back to zero bytes. After doing this a few time, the previous heapshot goes always back to zero bytes.


Because in cocos2d everytime you replace a scene everything attached to that scene is deallocated, and when you come back to it, everything is allocated again. So I think there will always be an heap growth because that growth is the size of the Scene with that layer as a child. Am I correct?


Also i noticed that everytime I did this, the #Living column in the statistics was not increasing, which is a good sign, right?

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement