Dealloc导致Cocos2D中场景之间的更改崩溃

时间:2013-08-18 12:30:43

标签: ios objective-c cocos2d-iphone exc-bad-access

我正在构建一个Cocos2D游戏,其中两个特定的精灵碰撞后(通过简单的边界框技术),我打电话

[[CCDirector sharedDirector] replaceScene:gameOverScene];

改变场景中的游戏。

一旦用游戏中的所有内容初始化游戏,游戏崩溃并转到ccArray.m类中的此方法:

void ccArrayRemoveAllObjects(ccArray *arr) { while( arr->num > 0 ) CC_ARC_RELEASE(arr->arr[--arr->num]); }

带有消息: 线程1:程序收到信号:“EXC_BAD_ACCESS”。

我尝试使用断点进行调试,并发现一旦我的gameOver场景初始化并准备好显示,前一个类中的dealloc方法(称为替换场景的游戏类)被调用,之后它引发了这个错误。

我的update方法:

-(void)update:(ccTime)dt {

if (CGRectIntersectsRect(plane.boundingBox, enemy.boundingBox)) {
CCScene *gameOverScene = [GameOverLayer sceneWithWon:NO]; [[CCDirector sharedDirector] replaceScene:gameOverScene]; } }

我的dealloc方法:

- (void) dealloc {

[super dealloc];

[_monsters release];
_monsters = nil;
[mole release];
mole = nil;
[text release];
text = nil;
[mBG1 release];
mBG1 = nil;
[mBG2 release];
mBG2 = nil; }

我不知道为什么会这样。请帮忙。

在期待中感谢你。

2 个答案:

答案 0 :(得分:1)

在释放游戏场景之前,你正在调用super的dealloc。 在你的dealloc方法结束时调用super的dealloc:

- (void) dealloc {   
   [_monsters release];
   _monsters = nil;
   [mole release];
   mole = nil;
   [text release];
   text = nil;
   [mBG1 release];
   mBG1 = nil;
   [mBG2 release];
   mBG2 = nil; 

   [super dealloc];
}

当您首先调用超级dealloc时,您发布的实例会使其成员无效,因此在dealloc之后访问它们会导致内存不良访问问题

答案 1 :(得分:1)

因为你没有显示足够的代码,我只能猜测这些对象中的一些,例如_monsters,mole,text,mBG1,mBG2,在你在dealloc功能中释放它们之前是自动释放的。所以,它们不能被释放了。这是关于ARC自动引用计数你应该知道的,它是ios的基础。 试试这个:

- (void) dealloc {

    [super dealloc];

}
相关问题