2011-09-23 4 views
1

이것은 Xcode 4를 사용하여 Objective-C로 작성된 iPhone APP를위한 것입니다.함수에서 반환되는 객체를 해제합니까?

빠른 질문은 해당 함수에서 ALLOC'ed 인 NSArray를 반환하는 함수가 있다면 해제해야합니까?

자세한 질문입니다.

내 아이폰 애플 리케이션에 "분석"실행하고 내 기능

기능이있는 NSMutableArray 밖으로있는 NSArray를 생성하고있는 NSArray를 반환 중 하나에 가능한 메모리 누수에 대한 불평. 내가 뭘하고있는 클래스 개체의 NSMutableArray 복용 및 NSString 중 NSArray 만드는 중. 가능한 한 코드를 간소화하여 문제를 보여 주므로 유용하지 않은 것처럼 보이는 경우 걱정하지 마십시오.

-(NSArray *)encodeArray 
{ 
    // I use a NSMutableArray here because I do not know how big the starting 
    // array will be (I hard coded the 20 here for now) 
    NSMutableArray *tmp = [[NSMutableArray alloc]init ]; 

    for (int y = 0;y<20;y++) { 
     // create the NSString object and add it to the tmp array 
     NSString *cardcount = [NSString stringWithFormat:@"%i%",y]; 
     [tmp addObject:cardcount]; 
    } 
    // create the array we will be returning out of the NSMutableArray 
    NSArray *array = [[NSArray alloc] initWithArray:tmp copyItems:YES]; 
    // release the tmp array we created. 
    [tmp release]; 

    // return our array 
    // This is the location of the potential memory leak. SHOULD I RELEASE THIS 
    // If I DO - HOW DO I RETURN IT. 
    return array; 
} 

배열을 해제해야합니까? 그렇다면 어떻게 여전히 반환 할 수 있습니까? 어쩌면 내가하고있는 일을 수행하는 더 좋은 방법이 있을까요?

전반적인 목표는 응용 프로그램 상태를 저장하기 위해 NSUserDefaults를 사용할 수 있도록 NSArray를 만드는 것입니다.

답변

1

을; 만약 당신이 (에 의해 당신은 객체의 현재 범위를 의미합니다.) 객체를 보유/복사/etc하면 어딘가에서 그것을 풀어 놓아야합니다. encodeArray을 호출 한 사람은 누구나 array에 보존하지 않았으므로 릴리스하지 않아도됩니다.

-(NSArray *)encodeArray 
{ 
    // I use a NSMutableArray here because I do not know how big the starting 
    // array will be (I hard coded the 20 here for now) 
    NSMutableArray *tmp = [[NSMutableArray alloc] init]; 

    for (int y = 0;y<20;y++) { 
     // create the NSString object and add it to the tmp array 
     NSString *cardcount = [NSString stringWithFormat:@"%i%",y]; 
     [tmp addObject:cardcount]; 
    } 

    // create the array we will be returning out of the NSMutableArray 
    // Named initializers indicate that the object will be autoreleased: 
    NSArray *array = [NSArray arrayWithArray:tmp]; 

    // release the tmp array we created. 
    [tmp release]; 

    // return our array 
    return array; 
} 

또는 : : 따라서 반환되기 전에 오토 릴리즈하도록 설정해야하는 설명은

-(NSArray *)encodeArray 
{ 
    // I use a NSMutableArray here because I do not know how big the starting 
    // array will be (I hard coded the 20 here for now) 
    NSMutableArray *tmp = [[NSMutableArray alloc] init]; 

    for (int y = 0;y<20;y++) { 
     // create the NSString object and add it to the tmp array 
     NSString *cardcount = [NSString stringWithFormat:@"%i%",y]; 
     [tmp addObject:cardcount]; 
    } 
    // create the array we will be returning out of the NSMutableArray 
    NSArray *array = [[NSArray alloc] initWithArray:tmp]; 

    // release the tmp array we created. 
    [tmp release]; 

    // return our array 
    return [array autorelease]; 
} 
+0

감사합니다 및 샘플 코드는 백업. 뿐만 아니라 내 문제를 해결했지만, 나는 또한 뭔가를 배웠습니다. – Johnne

3

일반적으로이 경우 자동으로 릴리스 된 배열을 반환하려고합니다. 함수에서 해제하면 호출자가 자신의 복사본을 보유 할 기회가 있기 전에 할당이 해제됩니다. 자동 포기하지 않으면 호출자가 반환 된 객체를 보유 할 때 보유 개수가 2이고 누출 될 수 있습니다.

그래서, 그냥이에 return 문을 변경 : 엄지 손가락의 규칙으로

return [array autorelease]; 
관련 문제