2010-08-08 44 views
0

我用下面的代码给出了一个在pics对象中的memroy泄漏,它显然与对象imageName相关。NSString内存泄漏?

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

} 

我读过关于内存管理bu的Apple文档我找不到我在那里做错了什么......任何想法?

干杯,

蒂比。

+0

'imageName'不会被释放,直到'纹理'被释放。你确定'纹理'最终被释放吗? – David 2010-08-08 12:56:50

+0

哦,不应该是'[纹理addObject:图片]'?当纹理被添加时,'纹理'会自动保留图片 - 而且我没有把'pics'的所有权传递给其他任何东西。 – David 2010-08-08 12:58:09

回答

1

如果纹理是一个NSMutableArray,那么你的[纹理addObject:]调用已经发送一个保留到图片。所以,代码应该是:

[textures addObject:pics];