2011-05-11 4 views
1

의 UIViewController 클래스 ...Memorymanagement ... NSObject의 클래스에서 나는 다음과 같은 코드 누출 메모리 문제가

@property (nonatomic, retain) NSMutableArray *childrensArray; 


-(void)connectionDidFinishLoading:(NSURLConnection *)connection { 

NSLog(@"Connection finished loading."); 
// Dismiss the network indicator when connection finished loading 
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO; 

// Parse the responseData of json objects retrieved from the service 
SBJSON *parser = [[SBJSON alloc] init]; 

NSString *jsonString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]; 
NSDictionary *jsonData = [parser objectWithString:jsonString error:nil]; 
childrensArray = [jsonData objectForKey:@"Children"]; 

// Callback to AttendanceReportViewController that the responseData finished loading 
[attendanceReportViewController loadChildren]; 

[connection release]; 
[responseData release]; 
[jsonString release]; 
[parser release]; 
} 

의 ViewController에서 다음 또한 메모리 누수에있는 NSMutableArray를 가져올 때

@property (nonatomic, retain) NSMutableArray *childrensArray; 


- (void)loadChildren { 

// Retrieve a array with dictionaries of children from ServiceGetChildren 
self.childrensArray = [[serviceGetChildren.childrensArray copy] autorelease]; 

int total = [childrensArray count]; 
totalLabel.text = [NSString stringWithFormat:@"%d", total]; 

[theTableView reloadData]; 
} 
+1

게시자 코드를 삭제할 수 있습니까? – toto

+0

방금 ​​말했듯이, 당신의 누출 라인 ('childrensArray = [serviceGetChildren.childrensArray copy];''''대신'{')이 있습니다. 한 번에 붙여 넣기 복사 할 수 있지만 복사하여 붙여 넣을 수 있습니다. 레이아웃이 완전히 동일하면 더 많이 ... 아휴, 사물, 잘못이 있습니다. – Joetjah

+0

@toto 물론 미안합니다. 이제는 더 읽기 쉬울 것입니다. – Silversnail

답변

1

인스턴스가 할당 해제 될 때 childrensArray 만 릴리스합니다. 또한 설정하기 전에 인스턴스 변수를 해제해야합니다

- (void)loadChildren { 
    // Retrieve a array with dictionaries of children from ServiceGetChildren 
    [childrensArray release]; 
    childrensArray = [serviceGetChildren.childrensArray copy]; 
} 

더 좋은 방법은 실제로 당신의 재산을 사용하는 것입니다 :

- (void)loadChildren { 
    // Retrieve a array with dictionaries of children from ServiceGetChildren 
    self.childrensArray = [[serviceGetChildren.childrensArray copy] autorelease]; 
} 

이의 이점이

합니다 (autorelease 참고) KVO 알림을 실행하면 사용할 수 있습니다.

+0

또 다른 문제는 childrensArray 인스턴스 변수를 해제하지 않고 덮어 쓰는'childrensArray = [jsonData objectForKey : @ "Children"];'입니다. – Tony

+0

그래, 만약 내가 self.childrensArray = [ [serviceGetChildren.childrensArray copy] autorelease]; dealloc에서 해제해야하지 않습니까? – Silversnail

+0

@Tony Ok하지만 임시 변수를 사용하는 경우 그런 다음 viewController에서 검색 할 수 있습니까? 나는 아직도 그 재산을 정하고 있습니까? 또는 어떻게 의미합니까? – Silversnail