我正在使用cocos2d编写代码。 我想释放我分配的所有内存。我用下面的方法在dealloc方法中完成了它。
我发布的所有对象都在接口文件中声明,属性(分配)已设置并在实现文件中合成。
我用的alloc方法像编程接收信号:“EXC_BAD_ACCESS”
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];
}
创建他们,但我得到:程序接收到的信号:“EXC_BAD_ACCESS”。 我调试它显示错误在[超dealloc];
我在内存管理上完全错了吗?或者我错过了这件事。 谢谢。
但是,我读了多少次我们分配内存,我们应该多次释放。所以,我做到了。我会通过你发布的链接。 谢谢。 – 2010-06-22 10:17:04
Srikanth - 是的,对象应该被“释放”多次,因为它已被“保留”(或分配)。然而,简单地释放一个对象反复击败了引用计数点 - 正如你基本上所说的“我不在乎这被引用了多少次,只是释放这个对象所使用的内存”。 想象一下你有A,B和Z的情况.A和B都参考Z.你创建Z,然后A,然后B,所以现在Z的refcount是2. 然后说释放A,得到处理。然后它会释放Z两次,Z会得到处理。如果B然后尝试使用Z,它将会失败。 – AlexC 2010-06-23 09:40:39