NSString内存泄漏?

时间:2010-08-08 12:53:29

标签: objective-c iphone nsstring

我正在转而使用下面的代码给我一个pics对象的memroy泄漏,显然是linke到对象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];

}

我已经阅读了有关内存管理的Apple文档,我找不到我在那里做错了什么......有什么想法吗?

干杯,

提笔。

1 个答案:

答案 0 :(得分:1)

如果textures是NSMutableArray,那么你的[textures addObject:]调用已经向pics发送了一个retain。所以,代码应该是:

[textures addObject:pics];