Memory leak problem

Can anyone ask for advice why I have a memory leak in this code?

I added a comment in Analyzer to the code. I would appreciate it if someone could help me and also explain why I get these two comments?

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

}

+3
source share
3 answers

Your first leak is caused by what you call:

[sortedDummyArray retain];

You already called alloc, which does this, but you only let it go once at the end (so delete the line above). Then you also reassign it, which is wrong.

, finalArray alloc . :

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

overkill , 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];
}
+3

dummySortedArray? , ,

NSArray * sortedDummyArray = [[NSArray Alloc] initWithArray: selectedPlayersArray];

+1

:

finalArray = [shufflePlayersFunction shufflePlayers: finalArray];

Previously, you create a new array in memory and point finalArray to it. But this line here tells finalArray to point to something else. So now this original array is still in memory, plus you are releasing another array.

You also do the same with DummyArray sorting. If you select an object, do not set the pointer to anything else.

+1
source

All Articles