I'm not very good at memory management, so I'll start with that as my disclaimer, but I did try to find out as much as I could about my current problem. I'm using Spritebuilder to make an app (uses cocos2d) and whenever I switch between two scenes I'm getting a slow memory build up in my game. I am looking at the "Memory" tab under the Debug Navigator.
Now I created a new project in Spritebuilder to see if a new app would also have this problem. It didn't. So clearly there is something going on in mine specifically.
I tracked it down a little further. One of the scenes in my game I'm switching to is just a simple menu screen with a couple CCButtons, and the other is a larger scrollable level map with many CCButtons, many textures...etc. Clearly the memory would always build up when I switched back to the more complex level scene. I have about 60 CCButtons on that level scene, and decided to remove them all and do the same test. This time, with all CCButtons removed, I did not see a memory build up (or at least it was building up at 1/100 the amount every time I switched). So I think that is the main cause of this memory build up - the way I'm allocating memory for my buttons.
I opened up the instruments tool in Xcode and looked for leaks, but did not find any (not super good at using this, but it seemed somewhat straightforward). So I feel like I am just allocating memory that is maybe not necessary?
Also, I'm not too sure what the Memory tab exactly means. Is it a problem if this builds up as I use my app more and more? Below is the method I use to initialize a button within my level scene.
-(void)createButtonAt:(CGPoint)buttonLocation buttonTitle:(NSString*)buttonTitle buttonNumber:(int)buttonNumber{
tempButton = [CCButton buttonWithTitle:buttonTitle
spriteFrame:redTile
highlightedSpriteFrame:redTile
disabledSpriteFrame:nil];
tempButton.label.color = [CCColor colorWithRed:0 green:0 blue:0];
tempButton.block = ^(id sender) {
for (int i = 1; i < buttonNumber; i++) {
[[LevelManager sharedInstance] nextLevel];
}
levelNumber = i;
[self loadMyViewController];
};
tempButton.scale = 0.3
tempButton.label.fontName = @"HelveticaNeue-Bold";
tempButton.label.fontSize = 34;
tempButton.anchorPoint = ccp(0.5, 0.5);
tempButton.position = buttonLocation;
[self addChild:tempButton];
[buttonArray addObject:tempButton];
}
When first loading the game I would be at 14 MB. After loading all of my levels and scenes, I would be around 60 MB. Then every time I played a level after that, the memory would increase 6-7 MB. Is this a problem? Please let me know if there is a specific reason this is happening, or some resources for how to fix this. Thanks!
If i recall, i think that the issue is with setBlock
which causes a retain cycle. Try this:
__weak id _weakSelf = self;
tempButton.block = ^(id sender) {
for (int i = 1; i < buttonNumber; i++) {
[[LevelManager sharedInstance] nextLevel];
}
levelNumber = i;
[_weakSelf loadMyViewController];
};
obcit: from memory, not tested, but should work.