2010-08-08 7 views
0

다음 코드를 사용하여 사진 개체에 memroy 누수가 발생했습니다. 분명히 linke를 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]; 

} 

메모리 관리에 대한 Apple 문서를 읽었습니다. 내가 잘못한 것을 찾을 수 없습니다.

건배,

티비.

+0

'imageName'은'텍스처 '가 할당 해제 될 때까지 할당 해제되지 않습니다. '텍스쳐 '가 결국 출시 될 예정입니까? – David

+0

아, 그러면 [[textures addObject : pics]'가 아니겠습니까? '텍스처 '는 추가 될 때 사진을 자동으로 유지합니다. 그리고 당신이 볼 수있는 한'사진 '의 소유권을 다른 것으로 넘기지 않을 것입니다. – David

답변

1

텍스처가 NSMutableArray 인 경우 [textureures addObject :] 호출은 이미 사진을 보유하도록 보냅니다. 따라서 코드는 다음과 같아야합니다.