2010-08-08 87 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];