Cocoa touch 程序接收信号:“;EXC#u坏访问”;

Cocoa touch 程序接收信号:“;EXC#u坏访问”;,cocoa-touch,cocos2d-iphone,dealloc,Cocoa Touch,Cocos2d Iphone,Dealloc,我正在使用cocos2d编写代码。 我想释放我分配的所有内存。我已经用dealloc方法按以下方式完成了。 我发布的所有对象都在接口文件中声明,属性(assign)已设置并在实现文件中合成。 我使用alloc方法创建它们,就像 self.PlayerA = [[CCSprite alloc] initWithFile:@" PlayerImage_01.png"]; -(void)dealloc { int count , i ; count = [self.PlayerA retai

我正在使用cocos2d编写代码。 我想释放我分配的所有内存。我已经用dealloc方法按以下方式完成了。
我发布的所有对象都在接口文件中声明,属性(assign)已设置并在实现文件中合成。
我使用alloc方法创建它们,就像

self.PlayerA = [[CCSprite alloc] initWithFile:@" PlayerImage_01.png"];


-(void)dealloc
{
 int count , i ;

 count = [self.PlayerA retainCount];
 for(i = 0; i < count; i++)
   [self.PlayerA release];

 count = [self.targetLayer retainCount];
 for(i = 0; i < count; i++)
   [self.targetLayer release];

  count = [self.playerGunSlowDrawSheet retainCount];
 for(i = 0; i < count; i++)
    [self.playerGunSlowDrawSheet release];

 count = [self.playerGunSlowDrawAnimation retainCount];
 for(i = 0; i < count; i++)
    [self.playerGunSlowDrawAnimation release];

 count = [self.numberFinishedTime retainCount];
 for(i = 0; i < count; i++)
    [self.numberFinishedTime release];

 count = [self.backGroundImage retainCount];
 for(i = 0; i < count; i++)
   [self.backGroundImage release];

 [[CCTextureCache sharedTextureCache] removeAllTextures];
 [[CCSpriteFrameCache sharedSpriteFrameCache] removeSpriteFrames];

 [super dealloc];
 }
self.PlayerA=[[CCSprite alloc]initWithFile:@“PlayerImage_01.png”];
-(无效)解除锁定
{
int计数,i;
计数=[self.PlayerA retainCount];
对于(i=0;i
但我得到:程序收到信号:“EXC\u坏访问”。 I调试器它在[super dealoc]处显示错误

我在内存管理方面完全错了吗?还是我在这件事上遗漏了什么。
谢谢。

是的,这不是释放对象的好方法。通过在dealloc方法中多次释放对象(如果retain count>1),就有可能将地毯从可能使用对象的任何其他对象下拉出

如果您拥有该对象,则只应释放该对象一次(假设您只保留该对象一次或自己分配)

有关“拥有”对象的更多信息,我建议您阅读Apple的内存管理编程指南,网址为:

一旦您了解了Objective-C内存管理的基础知识,使用XCode的Build菜单中的Build and Analyze(构建和分析)项将有助于发现类似这样的问题

通常,dealloc方法应如下所示:

-(void) dealloc
{
    [self.objectA release];
    [self.objectB release];

    [super dealloc];
}

编辑:为了回答您最初的问题,即您为什么获得EXC_BAD_访问权限,这里没有足够的信息来确定原因。一个可能的原因可能是您正在解除分配子类中的对象,该对象也在您的超类的dealloc方法中被解除分配。但这只是一个不确定的事实

但是,我读到我们应该释放多少次内存。所以,我做到了。我会浏览你发布的链接。谢谢。Srikanth-是的,对象应该被“释放”的次数和它被“保留”(或分配)的次数一样多。然而,简单地释放一个对象会反复破坏引用点计数——正如您所说的“我不在乎它被引用了多少次,只要释放这个对象使用的内存就行了”。假设有一个例子,a,B和Z。a和B都参考Z。你创建了Z,然后是a,然后是B。所以现在Z的refcount是2。然后说你释放了一个,并导致它被解除分配。然后它将释放Z两次,Z将被解除分配。如果B然后尝试使用Z,它将失败。