Cocos2d-iphone not calling dealloc when replacing scene

This is a simplified version of the problem that I am facing now. I made 2 empty CCScene 1 and 2 and added CCLayer 1 and 2 to their respective scene. I also added a touch function to switch from scene 1 to scene 2 using the CCDirector replaceplacecene.

However, dealloc was never called during a scene change.

// scene & layer 2 are exactly the same as 1 @implementation MainScene -(void)dealloc { NSLog(@"scene dealloc"); [super dealloc]; } -(id)init { self = [super init]; if (self) { layer = [[MainLayer alloc]init]; [self addChild:layer]; [layer release]; NSLog(@"test: %i", [layer retainCount]); //1 } return self; } @implementation MainLayer -(void)dealloc { NSLog(@"layer dealloced"); [super dealloc]; } -(id)init { self = [super init]; if (self) { self.isTouchEnabled = YES; NSLog(@"test %i", [self retainCount]); //1 } return self; } -(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { NSLog(@"test %i", [self retainCount]); //2 --> ???? [[CCDirector sharedDirector] replaceScene:[[SecScene alloc]init]]; } 

in addition, NSLog reported that the level of the remainder of the layer is 2 when I touch the screen. Is this even happening? Can someone probably tell me what I did wrong, or is it just my misunderstanding that keepCount must be 0 before dealloc is called?

This problem causes my main game program to crash, just switching between different scenes / layers using only static sprites (and some minor actions) over and over again.

+4
source share
2 answers

I don’t know much about the cocos2d contract, but you shouldn’t let go of the SecScene that you allocate to ccTouchesBegan on this line: [[CCDirector sharedDirector] replaceScene:[[SecScene alloc]init]]

I see no reason why replaceScene would not save, so SecScene now has a retention value of two when it should have one.

More importantly, if you added MainScene similar way to explain why its storage counter is larger than you would like, so it will never be released.

+2
source

Also, dealloc only gets called rarely, I found - so it's hard to check and make it call ...

0
source

Source: https://habr.com/ru/post/1314292/


All Articles