views:

78

answers:

2

Hi there, 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.

A: 

Hi theren and thanks for your answer. Actually this code is a class that is instantiated by another class to be release later (in the calling class). So Normally textures should be released. For the [pics retain] I was using that because in the Apple doc, it is said that textures might have problems

heisenObject = [[array objectAtIndex:n] retain]; [array removeObjectAtIndex:n];

// use heisenObject.

[heisenObject release];

Tibi
+1  A: 

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

[textures addObject:pics];
Scott Gruby