[ios]内存泄漏问题

发布时间: 2017/3/21 22:40:59
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

有人请为什么我在这段代码有内存泄漏的建议好吗?

在代码中,我有分析器中添加批注。如果有人可以帮助我所解释,为什么我得到这两个意见,我会感激吗?

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

}

解决方法 1:

你的第一个漏洞是由于你调用造成的︰

[sortedDummyArray retain];

你已经被称为分配,这样做的但你只释放它一旦在结束了 (所以删除上面的行) 你也然后重新分配,这是不正确的。

你的第二次泄漏被导致因为你有设置与分配 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];
}

所有这一切都是太夸张,但只是要删除重复的条目,将您的数组转换为一个可编程 (这始终是唯一的),然后将它转换回 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];
}
官方微信
官方QQ群
31647020