0

this is my first post so don't go too rough on me.

I have a problem with cocos2d. Im making a game with a HUD layer and a game layer. When I call replace main menu scene with [ClassicGameLayer scene] my HUD and game layer get init-ed this way:

+ (CCScene*)scene
{
CCScene *scene = [CCScene node];

HudLayer *hud = [[[HudLayer alloc] initWithMode:1] autorelease];
ClassicGameLayer *layer = [[[ClassicGameLayer alloc] initWithHUD:hud] autorelease];

[scene addChild:hud z:hudZ];
[scene addChild:layer z:layerZ];

return scene;
}

and when the user fails the game HUD layer calls

[[CCDirector sharedDirector] replaceScene:[GameOverLayer sceneWithMode:integer andScore:points]]];

dealloc of the HUD layer gets called but dealloc of the ClassicGameLayer is never called. I googled almost everything I could think of but still no luck. Does anybody know whats causing me this problem? And if so how can I fix it? Every other scene is being released properly i think :)

Majster
  • 3,611
  • 5
  • 38
  • 60
  • why do you care if dealloc is called? You are using autorelease on both of them, so its up to the system to decided when to garbage collect it – cpjolicoeur Apr 27 '12 at 18:30
  • Cause they seem to be causing memory issues. Game crashes after playing it for some time. I think that memory management is causing this problem. – Majster Apr 27 '12 at 18:40
  • 1
    if you are concerned with the memory management explicitly, then dont use autorelease. Just use manual retain/release and release the object your self when you are done with it. – cpjolicoeur Apr 27 '12 at 18:41
  • You can run a NSLog in "applicationDidReceiveMemoryWarning" then you'll know for sure. – Mick MacCallum Apr 27 '12 at 18:44

1 Answers1

0

If your autorelease pool never gets to the place where it deallocates stuff, it will never release any memory. I had this issue once with a Mac app I was writing. It won't show up as a memory leak in Instruments, either.

I recommend not using autorelease if you are having this problem.

Almo
  • 15,538
  • 13
  • 67
  • 95
  • Thanks for your answer. The only question not left is how? If I change to just `[[ClassicGameLayer alloc] initWithHUD:hud]` how am I supposed to release it? HUD replaces the scene so my classic game layer "doesn't know" when its getting replaced. Should I override `- (void)onExit` method? – Majster Apr 28 '12 at 08:58
  • I can't really answer this without seeing how the program is structured. – Almo Apr 30 '12 at 13:20