2011-05-04 4 views
0

plist에서 내 애플리케이션의 테이블 뷰로 데이터를로드하고 있습니다. 데이터는 변경 가능한 사전의 변경 가능한 사전에 저장됩니다. 사용자가 카테고리를 삭제할 수 있도록테이블 뷰에서 행을 삭제할 때 메모리 누수가 발생했습니다.

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    self.title = @"Categories"; 

    // load data from plist fle 
    self.categories = [[[NSMutableDictionary alloc] initWithContentsOfFile: 
            [[NSBundle mainBundle] pathForResource:@"InventoryItems" ofType:@"plist"]] autorelease]; 

    // add buttons to navigation menu 
    self.navigationItem.rightBarButtonItem = self.editButtonItem; 
    self.navigationItem.leftBarButtonItem = self.addButton; 
} 

내있는 tableview은, 편집 할 수 : 여기 내있는 viewDidLoad 방법입니다. forRowAtIndexPath : 내 commitEditingStyle의 메소드 내 데이터 모델을 업데이트 : 나는 내 응용 프로그램을 프로파일 링 할 때

[self.categories removeObjectForKey: [[self.categories allKeys] objectAtIndex:indexPath.row]]; 

는 메모리 누수. 나는 프로파일 도구를 사용하는 데 능숙하지 않지만 행을 삭제할 때마다 내 범주 사전에 누수가있는 것 같습니다.

내가 놓친 부분이 어디 있는지 궁금합니다. 제가 제거하는 객체가 사전이기도하고 객체를 제거해야하는 것이 문제입니까?

답변

3

이 누출 (재산을 유지하거나 사본 인 경우) :

self.categories = [[NSMutableDictionary alloc] initWithContentsOfFile: 
            [[NSBundle mainBundle] pathForResource:@"InventoryItems" ofType:@"plist"]]; 

이 대신 사용 :

categories = [[NSMutableDictionary alloc] initWithContentsOfFile: 
            [[NSBundle mainBundle] pathForResource:@"InventoryItems" ofType:@"plist"]]; 

나이 :

self.categories = [[[NSMutableDictionary alloc] initWithContentsOfFile: 
            [[NSBundle mainBundle] pathForResource:@"InventoryItems" ofType:@"plist"]] autorelease]; 
+1

누출 뒤에 이유는 초기화가 만 1 누출을 예상 할 때 2의 유지 수를주고,도 1의 유지 카운트가 생성하는 하나의 수를 유지하고, 재산에 유지 생성하는 것입니다 . –

+0

빠른 답변 주셔서 감사합니다! self.categories에 autorelease를 추가했지만 여전히 같은 문제가 있습니다. 유출 된 개체에는 _NSCFDictionary 및 NSCFString이 포함됩니다. – lanan

+0

@Shvetusya 사전을 만드는 방법과 그 안에서 개체를 제거하는 방법을 보여 주었지만이 개체를 사용하여 테이블보기에 데이터를 표시해야합니다. 어쩌면 당신은 그 객체들을 (그냥 추측하는 것) 과도하게 유지할 것입니다. – albertamg

0

이 줄 것이라고 궁금 너 뭐 다른거야?

NSMutableDictionary *d = [[NSMutableDictionary alloc] initWithContentsOfFile: 
            [[NSBundle mainBundle] pathForResource:@"InventoryItems" ofType:@"plist"]]; 

self.categories = d; 
[d release]; 
관련 문제