PeterK
PeterK

Reputation: 4301

Memory leak problem

Could someone please advice why i have a memory leak in this code?

I have added the comment in Analyzer in the code. I would appreciate if someone could help me as well as 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];

}

Upvotes: 3

Views: 359

Answers (3)

theChrisKent
theChrisKent

Reputation: 15099

Your first leak is caused because you call:

[sortedDummyArray retain];

You have already called an alloc which does this, but you only release it once in the end (so remove the above line) You also then reassign it which is incorrect.

Your second leak is caused because you have set finalArray with an alloc and then replaced it with the results of the function. You can fix this by replacing this line:

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

With this one:

NSMutableArray *finalArray = [NSMutableArray arrayWithArray:sortedDummyArray];

And then removing this line:

[finalArray release];

So altogether your function would look like this:

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

But all of this is overkill just to remove duplicate entries, converting your array to an NSSet (which is always unique) then converting it back to an NSArray should take care of this for you, so your function should be:

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

Upvotes: 3

GendoIkari
GendoIkari

Reputation: 11914

Your problem is this line:

finalArray = [shufflePlayersFunction shufflePlayers: finalArray];

Earlier, you are creating a new array in memory, and telling finalArray to point to it. But this line here tells finalArray to point to something different. So now, that original array you created is still around in memory, plus you are releasing a different array.

You also do the exact same sort of thing with sortedDummyArray. If you alloc an object, don't then set the pointer to point to something else.

Upvotes: 1

JFoulkes
JFoulkes

Reputation: 2429

Is the call to retain dummySortedArray needed? I think you may already have a retain count on it when you initalise the collection

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

Upvotes: 1

Related Questions