gpt4 book ai didi

iphone - 内存泄漏问题

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:24:26 25 4
gpt4 key购买 nike

有人可以告诉我为什么我在这段代码中有内存泄漏吗?

我在代码中添加了 Analyzer 中的注释。如果有人可以帮助我并解释为什么我会收到这两条评论,我将不胜感激?

- (void)viewDidDisappear:(BOOL)animated {

// Empty array to be sure it is empty
[playerArray removeAllObjects];

//============CLEAN OUT DOUBLE NAMES FROM ARRAY==============//
NSArray *sortedDummyArray = [[NSArray alloc] initWithArray:selectedPlayersArray];
////>>>>The line above is line 84<<<<<<<////

// Sort the array
sortedDummyArray = [sortedDummyArray sortedArrayUsingSelector:@selector(compare:)];

NSMutableArray *finalArray = [[NSMutableArray alloc]initWithArray:sortedDummyArray];
////>>>>>> Possible memory leak on line 84 <<<<<<<<////



int xx = [sortedDummyArray count];
int yy;
int counter = 0;
int rr = 0;

for (int oo = 0; oo < xx; oo++) {
yy = [finalArray count];

for (int zz = 0; zz < yy; zz++) {

// If hit, clean out the double name
if ([[sortedDummyArray objectAtIndex:oo] isEqualToString:[finalArray objectAtIndex:rr]]) {

counter++;

// Check if there is more than one of this name
if (counter > 1) {
[finalArray removeObjectAtIndex:rr];
rr--;
counter--;
}
}
rr++;
}
counter = 0;
rr = 0;
}

[sortedDummyArray retain];

// Save who is in the game

AccessQuestionsDB *shufflePlayersFunction = [AccessQuestionsDB new];
finalArray = [shufflePlayersFunction shufflePlayers: finalArray];
[shufflePlayersFunction release];

TempPlayersInTheGame *savePlayersInTheGame = [TempPlayersInTheGame new];
[savePlayersInTheGame saveSelectedPlayers:finalArray];
[savePlayersInTheGame release];

[finalArray release]; //>>>> see comment below
////>>>>>Incorrect decrement of the reference count of an object that is not owned at this point by the caller <<<<<</////

[sortedDummyArray release];
[super viewDidDisappear:animated];

最佳答案

您的第一次泄漏是因为您调用:

[sortedDummyArray retain];

您已经调用了执行此操作的分配,但您最后只释放了一次(因此删除上面的行)您还重新分配了它,这是不正确的。

您的第二次泄漏 是因为您使用 alloc 设置了 finalArray,然后将其替换为函数的结果。您可以通过替换此行来解决此问题:

NSMutableArray *finalArray = [[NSMutableArray alloc]initWithArray:sortedDummyArray];

有了这个:

NSMutableArray *finalArray = [NSMutableArray arrayWithArray:sortedDummyArray];

然后删除这一行:

[finalArray release];

所以你的函数看起来像这样:

- (void)viewDidDisappear:(BOOL)animated {

// Empty array to be sure it is empty
[playerArray removeAllObjects];

//============CLEAN OUT DOUBLE NAMES FROM ARRAY==============//
// Sort the array
NSArray *sortedDummyArray = [selectedPlayersArray sortedArrayUsingSelector:@selector(compare:)];

NSMutableArray *finalArray = [NSMutableArray arrayWithArray:sortedDummyArray];

int xx = [sortedDummyArray count];
int yy;
int counter = 0;
int rr = 0;

for (int oo = 0; oo < xx; oo++) {
yy = [finalArray count];

for (int zz = 0; zz < yy; zz++) {

// If hit, clean out the double name
if ([[sortedDummyArray objectAtIndex:oo] isEqualToString:[finalArray objectAtIndex:rr]]) {

counter++;

// Check if there is more than one of this name
if (counter > 1) {
[finalArray removeObjectAtIndex:rr];
rr--;
counter--;
}
}
rr++;
}
counter = 0;
rr = 0;
}

// Save who is in the game

AccessQuestionsDB *shufflePlayersFunction = [AccessQuestionsDB new];
finalArray = [shufflePlayersFunction shufflePlayers: finalArray];
[shufflePlayersFunction release];

TempPlayersInTheGame *savePlayersInTheGame = [TempPlayersInTheGame new];
[savePlayersInTheGame saveSelectedPlayers:finalArray];
[savePlayersInTheGame release];

[super viewDidDisappear:animated];
}

但所有这些都太过分了只是为了删除重复的条目,将您的数组转换为 NSSet(它始终是唯一的)然后将其转换回 NSArray 应该为您解决这个问题,所以你的函数应该是:

- (void)viewDidDisappear:(BOOL)animated {

// Empty array to be sure it is empty
[playerArray removeAllObjects];

//============CLEAN OUT DOUBLE NAMES FROM ARRAY==============//
NSSet *uniquePlayers = [NSSet setWithArray:selectedPlayersArray];

// Save who is in the game

AccessQuestionsDB *shufflePlayersFunction = [AccessQuestionsDB new];
NSArray *finalArray = [shufflePlayersFunction shufflePlayers: [uniquePlayers allObjects]];
[shufflePlayersFunction release];

TempPlayersInTheGame *savePlayersInTheGame = [TempPlayersInTheGame new];
[savePlayersInTheGame saveSelectedPlayers:finalArray];
[savePlayersInTheGame release];

[super viewDidDisappear:animated];
}

关于iphone - 内存泄漏问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5317830/

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