2011-01-27 5 views
0

방금 ​​누수를 통해 내 응용 프로그램을 실행했고 다음 코드가 누수를 일으킨다는 말을 듣고 있지만 어떻게 보이지 않습니다. 이 코드가 누출 되었습니까?

은이 코드 내 viewDidLoad 일부 NSMutableArray의 할당 :

[self.currentCars removeAllObjects]; 
[self.expiredCars removeAllObjects]; 
for (Car *car in [self.dealership cars]) { 
    if ([car isCurrent]) 
     [self.currentCars addObject:car]; 

    if ([car isExpired]) 
     [self.expiredCars addObject:car]; 
} 

그리고 나중에 코드 I에서 :

그런 다음
- (void)viewDidLoad { 
    [super viewDidLoad]; 
    self.currentCars = [[NSMutableArray alloc] init]; 
    self.expiredCars = [[NSMutableArray alloc] init]; 
} 

내가 내 다음 내 viewWillAppear 방법의 이러한 배열을 채울 다음 배열을 출시하십시오.

- (void) viewWillDisappear:(BOOL)animated { 

    if (currentCars != nil) { 
     [currentCars release], currentCars = nil; 
    } 
    if (expiredCars != nil) { 
     [expiredCars release], expiredCars = nil; 
    } 

    [super viewWillDisappear:animated]; 
} 

아이디어가 있으십니까? 감사!

+0

누수가 보이지 않습니다. 다른 곳에 있어야합니다. 관련 코드를 추가 할 수 있습니까? – BoltClock

+2

그 표본만을 근거로 말할 수는 없습니다. –

+0

아마도 나는 Instruments의 잘못된 위치를보고있을 것입니다. 그러나'[self.currentCars addObject : car]; 행을 hilights하고 그 행에 100 % 지시계를 놓습니다 ... 그냥 잘못 읽은 것입니까? –

답변

2

귀하의 누수가 여기에 있습니다 :

self.currentCars = [[NSMutableArray alloc] init]; 
self.expiredCars = [[NSMutableArray alloc] init]; 

는 속성이 같은 accessores 선언한다고 가정 : 제 생각에는

@property(nonatomic, retain) NSMutableArray *currentCars; 
@property(nonatomic, retain) NSMutableArray *expiredCars; 

을, (악기를 사용하는 것보다 다른) 누수를 찾을 수있는 가장 좋은 방법이다 보유 수를 수동으로 추적하십시오.

예를 들어 currentCars과 같이 처리하려는 경우 누출을 쉽게 발견 할 수 있습니다. 결과는 다음과 같습니다.

self.currentCars = [[NSMutableArray alloc] init]; 
// The 'init' makes the retain count 1. 

// 'self.currentCars = ..' translates to the setCurrentCars: method. 
// You probably did not implement that method yourself, 
// but by synthesizing your property it is automatically implemented like this: 
- (void)setCurrentCars:(NSMutableArray *)array { 
    [array retain]; // Makes the retain count 2 
    [currentCars release]; 
    currentCars = array; 
} 

// In your viewWillDisappear: method 
[currentCars release], currentCars = nil; // Makes the retain count 1 so the object is leaked. 

해결책은 간단합니다. 이것을 사용하십시오 :

NSMutableArray *tempMutableArray = [[NSMutableArray alloc] init]; 
self.currentCars = tempMutableArray; 
[tempMutableArray release]; 

작은 사이드 노트. 당신은 viewWillDisappear:에 당신의 물건을 풀어서는 안됩니다. 이를 수행하기위한 권장 장소는 dealloc입니다. 따라서 귀하의 코드는 다음과 같습니다 :

- (void)dealloc { 
    [currentCars release], currentCars = nil; 
    [expiredCars release], expiredCars = nil; 
    [super dealloc]; 
} 
+0

'-init' 호출은 유지 카운트를 변경하지 않습니다. Apple은'-alloc'을 호출하여 보유 수를 1로 설정합니다 (Apple이 적어도 보유 수를 소유하는 것이 아니라 소유권 측면에서 생각하는 것이 좋습니다). 보유 수를 계산하려는 시도는 가정에 달려 있기 때문에 항상 옳은 것으로 드러납니다. – jlehr

+0

또한, OP가 여기에서했던 것처럼 기본 인스턴스 변수가'nil'으로 설정되어있는 한 메모리 소비를 줄이려면'-viewWillDisappear :'에서 객체를 해제하는 것이 반드시 잘못된 것은 아닙니다. 한편, 인스턴스 변수를'-dealloc'에서'nil'으로 설정하는 것은 유용하지 않을 것입니다. – jlehr

0

당신이 currentCars, expiredCars, dealership 또는 cars 매우 이상한 일을하지 않는 한이, 아니, 아무 누출도 없다.

누출 위치에 대한 인스트루먼트의 포인터는 실제로 객체가 실제로 유출되는 곳이 아닙니다. 짐작할 수 있겠지만 dealloc 메서드에서 currentCars 또는 expiredCars 중 하나를 내놓지 않을 수도 있습니다.

+0

그래, viewWillDisappear가 호출되도록 보장되는지 궁금 하네. –

2

문제는 (아마도) -viewDidLoad에서 배열의 초기 설정에 대해 속성 접근자를 사용하고 있다는 것입니다. 잘 구현 된 속성 접근자가 객체를 유지하므로 + alloc에서 1 유지하고 할당에서 다른 유지를 얻습니다. 이 문제를 해결하려면 할당 한 후 배열을 해제하거나 [NSMutableArray array]을 사용하여 초기 할당에 사용할 자동 해제 된 배열을 가져와야합니다.