Jump to content

View more

Image of the Day

Adding some finishing touches...
Follow us for more
#screenshotsaturday #indiedev... by #MakeGoodGames https://t.co/Otbwywbm3a
IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

Sign up now

Instruments - can't find leak - Cocos2d

4: Adsense

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 muralhas1   Members   


Posted 12 February 2013 - 12:10 PM


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.

#2 arcademissile   Members   


Posted 12 February 2013 - 06:15 PM

A quick look seems like you are not calling [super dealloc]; in dealloc. See if that fixes it.

#3 arcademissile   Members   


Posted 12 February 2013 - 06:31 PM

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.

#4 muralhas1   Members   


Posted 13 February 2013 - 05:36 AM

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?

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.