我正在使用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