gpt4 book ai didi

iphone - NSMutableArray 释放插入的对象。 -> 导致内存泄漏

转载 作者:行者123 更新时间:2023-12-03 17:30:50 25 4
gpt4 key购买 nike

您好,我在删除可变数组的对象时遇到很多问题。

我有一个方法可以发回一个用自定义对象初始化的可变对象。这个可变的声明类似于 autorelease,用于在方法之后释放。在我返回时,我保留了可变值以免丢失它。我想在第二种方法中删除我的可变内容并释放我的可变内容。但我的应用程序退出并失败。

  //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];

我的应用程序正在退出。

你有办法避免内存泄漏并清理干净吗?

最佳答案

尝试使用 NSZombieEnabled 检查 EXC_BAD_ACCESS 的原因..

HowTo is found here..

关于iphone - NSMutableArray 释放插入的对象。 -> 导致内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4412997/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com