Max
Max

Reputation: 93

XCode Analyzer : Potential leak of an object

here is a piece of code, could you help me managing memory correctly in it please ?

- (void) buildSpritesWithName:(NSString*)sN {

    self.arrayPages = [[NSMutableArray alloc] initWithCapacity:self.numPages];
    NSString *spriteName0 = [NSString stringWithFormat:@"%@%i.png", sN, 0];
    CCSprite *temp0 = [CCSprite spriteWithSpriteFrameName:spriteName0];

    NSLog(@"temp sprite %@ size : %f / %f",spriteName0, temp0.contentSize.width, temp0.contentSize.height);

    self.imgSize = CGSizeMake(temp0.contentSize.width, temp0.contentSize.height);


    for (int c = 1; c <= numberOfWheelFacesCycles; c++) {

        for (int x = 1; x <= self.numPages; x++) {

            NSString *spriteName = [NSString stringWithFormat:@"%@%i.png",sN, x];
            CCSprite *temp = [CCSprite spriteWithSpriteFrameName:spriteName];

            [self.arrayPages addObject:temp];
        }
    }

    NSLog(@"%i Pages built",self.arrayPages.count);
}

Analyzer says "Potential leak of an object" at line :

NSString *spriteName0 = [NSString stringWithFormat:@"%@%i.png", sN, 0];

Why that ? NSString is autorelease right ? so which object could be leaked ?

I have the following in another class, again, what's the problem :

self.name = [playersNames objectAtIndex:type];

By the way, if the management of the loop is not good, could you advice as well ?

Thanks for your help

Upvotes: 1

Views: 6405

Answers (1)

Extra Savoir-Faire
Extra Savoir-Faire

Reputation: 6036

Look at your property or accessor for arrayPages. If it retains, then your potential leak arises when you call:

self.arrayPages = [[NSMutableArray alloc] initWithCapacity:self.numPages];

If arrayPages retains, then do this instead:

self.arrayPages = [[[NSMutableArray alloc] initWithCapacity:self.numPages] autorelease];

or more simply:

self.arrayPages = [NSMutableArray arrayWithCapacity:self.numPages];

As for your loop, it looks all right, but beware of creating a lot of autoreleased objects inside it; they aren't released until some point later.

Upvotes: 5

Related Questions