【发布时间】:2010-12-10 20:27:23
【问题描述】:
您好,我在删除可变数组的对象时遇到了很多问题。
我有一个方法可以返回一个用自定义对象初始化的可变变量。 这个可变的被声明为自动释放,用于在方法后释放。 在我的回报中,我保留了可变的不丢失它。 我想在第二种方法中删除我的可变内容并释放我的可变内容。 但是我的应用退出并失败了。
//first method which return my mutable
NSMutableArray *highScores = [[[NSMutableArray alloc] init]autorelease] ;
for (....)
{
HighScore *currentHighScore = [[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];
}
这是可行的,但当然我对未释放的可变对象中的所有对象都有内存泄漏。 但是如果我试图通过这个来释放可变对象和可变对象本身:
//remove Mutable array content.
//[highScoreList removeAllObjects] ;
//[highScoreList release];
我的应用正在退出。
你有没有办法避免内存泄漏并很好地清理它?
【问题讨论】:
-
如果您在应用程序崩溃时在调试中运行,控制台上会显示什么?堆栈跟踪上有什么?
-
你在哪里看到这个内存泄漏?程序在 2dn 情况下崩溃是显而易见的。您正在释放一个自动释放对象。
-
Filip:当我在调试方案 2(释放和删除对象)中运行时:EXC_BAD_ACCESS
-
Nils:如果没有场景 2,我们就会发生内存泄漏,因为我们在最后一个方法中保留它并且从不释放它。 ( [[HighScoreViewController getHighScores:NormalGameModeXML]retain] )
-
@nils 你在说什么自动释放的对象?我看不到他发布任何未正确保留的内容,除非他稍后在发布
highScoreList后尝试使用currentHighScore,但从未提及。
标签: iphone objective-c cocoa