Cocos2d iphone,替换场景时不调用dealloc
这是我现在面临的问题的简化版本。 我制作了两个空的CC1和CC2场景,并将CC1和CC2层添加到各自的场景中。 我还添加了一个触摸功能,使用CCDirector的ReplaceSecene从场景1切换到场景2 但是,在替换场景中从未调用deallocCocos2d iphone,替换场景时不调用dealloc,iphone,memory,cocos2d-iphone,Iphone,Memory,Cocos2d Iphone,这是我现在面临的问题的简化版本。 我制作了两个空的CC1和CC2场景,并将CC1和CC2层添加到各自的场景中。 我还添加了一个触摸功能,使用CCDirector的ReplaceSecene从场景1切换到场景2 但是,在替换场景中从未调用dealloc // scene & layer 2 are exactly the same as 1 @implementation MainScene -(void)dealloc { NSLog(@"scene dealloc"
// 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]];
}
此外,当我触摸屏幕时,NSLog报告层的重新计数为2。这是否真的会发生?有谁能告诉我我做错了什么,或者只是我的误解,在调用dealloc之前,retainCount必须为0
这个问题导致我的主游戏程序一次又一次地在不同的场景/层之间切换,只使用静态精灵(和一些小动作)就崩溃了。我对cocos2d的合同不太了解,但你不应该在这一行上释放
SecScene
你分配的ccTouchesStart
:[[CCDirector sharedDirector]replaceSecene:[SecScene alloc]init]
我看不出replaceSecene
没有保留的任何原因,因此现在SecScene
应该有一个保留计数时,它的保留计数是两个
更重要的是,如果您以类似的方式添加了
mainsecene
,这将解释为什么它的保留计数比您希望的要高一倍,因此它将永远不会被释放。此外,我发现很少调用dealloc-因此很难测试和调用它…在这种情况下,您很可能会在不到m的时间内泄漏内存ATCH alloc/release…这不是dealloc方法的错误,是什么、何时何地alloc/retain/release导致dealloc不被调用IP:使用自动释放,避免内存管理方面的许多麻烦!