Tutorial :NSString memory leak?



Question:

I'm turning around and around with the following code giving me a memroy leak in the pics object apparently linke to the object imageName.

for (int i = 0;i<[potatoesIndexesArray count];i++){         int imageNumber = [[potatoesIndexesArray objectAtIndex:i]intValue];        NSString *imageName = [[NSString alloc] initWithFormat:@"texture%d",imageNumber];        UIImage *image = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:imageName ofType:@"png"]];      //UIImage *imageHighlighted = [[UIImage alloc]initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:imageName ofType:@"png"]];        NSArray *pics = [[NSArray alloc] initWithObjects:                       [self maskImage:image withMask:[mainDelegate.masksArray objectAtIndex:i]],                       [self maskImage:image withMask:[mainDelegate.masksArray objectAtIndex:i]],                       imageName,                         nil]; // pics becomes owner of objects        [textures addObject:[pics retain]]; //textures becomes owner of pics. as a release occurs later. we must retaint pics to keep it available in textures.        [imageName release];      [image release];      [pics release];        //[imageHighlighted release];    }  

I've read the Apple doc on memory management bu I can't find what I did wrong there ... any idea ??

Cheers,

Tibi.


Solution:1

If textures is a NSMutableArray, then your [textures addObject:] call already sends a retain to pics. So, the code should be:

[textures addObject:pics];  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »