插入的NSMutableArray发布对象。 - >导致内存泄漏
问题描述:
嗨我有很多问题来删除我的可变数组的对象。插入的NSMutableArray发布对象。 - >导致内存泄漏
我有一个方法,发回可变初始化与自定义对象。 这个可变的声明像autorelease在方法之后释放。 在我的回报中,我保留可变的不松动。 我想在第二种方法中删除我的mutable的内容并释放我的mutable。 但我的应用程序退出并失败。
//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];
}
这是工作,但当然我有内存泄漏所有对象在mutable中没有发布。 但是如果我试图通过这个来释放可变和可变本身的对象:
//remove Mutable array content.
//[highScoreList removeAllObjects] ;
//[highScoreList release];
我的应用程序退出。
您是否有解决方案来避免内存泄漏并清理干净?
答
尝试使用NSZombieEnabled检查一个EXC_BAD_ACCESS的原因..
+0
很多,问题是dealloc我的高分(用户名)被释放,从未实例化..谢谢! – Tommy 2010-12-10 21:58:14
答
//[highScoreList removeAllObjects] ;
//[highScoreList release];
无需在发布之前。
请注意,如果在解除分配后使用highScoreList
,您的应用将按照您的描述崩溃。即如果您使用highScoreList
以上,BOOM。
您可以将highScoreList
设置为nil
,但更好的解决方案是理解为什么在您认为应该完成该操作后使用对象。
并一如既往:
如果您在调试时运行应用程序崩溃时,它在控制台上说了什么?堆栈轨迹上有什么? – filipe 2010-12-10 20:35:04
你究竟在哪里看到这种内存泄漏?该程序在2dn的情况下崩溃是显而易见的。你正在发布一个autorelease对象。 – nils 2010-12-10 20:36:45
Filip:当我在方案2(释放和删除对象)的调试中运行时:EXC_BAD_ACCESS – Tommy 2010-12-10 20:40:58