NSMutableArray release objects inserted. -> cause memory leak
Hi I have a lot of problems to remove the objects of my mutable array.
I have a method which send back a mutable initialized with a custom object. This mutable is declared like autorelease for releasing after method. In my return, I retain the mutable to not loose it. I want in this second method to remove the content of my mutable and release my mutable. But my app quit and fail.
//first method which return my mutable
NSMutableArray *highScores = [[[NSMutableArray alloc] init]autorelease] ;
for (....)
{
HighScore *currentH开发者_StackOverflowighScore = [[HighScore alloc] init];
currentHighScore.user = name;
currentHighScore.score = score;
//add to the array
[highScores addObject:currentHighScore];
[currentHighScore release];
}
return highScores;
// method which use the first method
//retrieve with retain to keep.
highScoreList = [[HighScoreViewController getHighScores:NormalGameModeXML]retain] ;
HighScore *currentHighScore;
int count = [highScoreList count];
for (int i = 0; i < count ; i++)
{
currentHighScore = [highScoreList objectAtIndex:i];
}
This is working, but off course I have memory leak for all the objects in the mutable not released. But if i'm trying to release the object of the mutable and the mutable itself by this :
//remove Mutable array content.
//[highScoreList removeAllObjects] ;
//[highScoreList release];
My app is quitting.
Do you have a solution to avoid the memory leak and clean it well?
Try using NSZombieEnabled to check the reason for an EXC_BAD_ACCESS..
HowTo is found here..
//[highScoreList removeAllObjects] ;
//[highScoreList release];
No need to removeAllObjects
prior to release.
Note that if you use highScoreList
after it is deallocated, your app will crash as you describe. I.e. if you use highScoreList
after the above, BOOM.
You could set highScoreList
to nil
, but a better solution is to understand why you are using an object after you think you should be done with it.
And, as always:
精彩评论