2010-11-09 29 views
0

我從viewDidLoad調用createTableData。我不明白的是我正在做一個NSMutableDictionary的分配,但我不明白爲什麼這個對象沒有從內存中釋放 - 儘管釋放。 我確實看到了內存泄漏,泄漏似乎指向了這部分代碼。 有人可以指向我可以讀/理解我應該做什麼與​​我正在做什麼的網址嗎?我似乎無法看到我在這裏出了什麼問題。如何dealloc NSMutableDictionary

- (void)createTableData { 
NSMutableArray *toolList; 
toolList=[[NSMutableArray alloc] init]; 
[toolList addObject:[[NSMutableDictionary alloc] 
    initWithObjectsAndKeys:@"Some title",@"name", 
      @"1",@"whatViewController", 
      @"",@"url", 
      @"some_icon.jpg",@"picture", 
      @"some detail text",@"detailText",nil]]; 
toolData=[[NSMutableArray alloc] initWithObjects:toolList,nil]; 
[toolList release]; 
} 

- (void)dealloc { 
    [toolData release]; 
    [super dealloc]; 
} 

回答

1
[toolList addObject:[[NSMutableDictionary alloc] 
    initWithObjectsAndKeys:@"Some title",@"name", 
      @"1",@"whatViewController", 
      @"",@"url", 
      @"some_icon.jpg",@"picture", 
      @"some detail text",@"detailText",nil]]; 

在這一行你加的NSMutableDictionary對象數組,而不是將其釋放。正確的代碼是(使用已經返回自動釋放的對象類方法):

[toolList addObject:[NSMutableDictionary 
    dictionaryWithObjectsAndKeys:@"Some title",@"name", 
      @"1",@"whatViewController", 
      @"",@"url", 
      @"some_icon.jpg",@"picture", 
      @"some detail text",@"detailText",nil]]; 

或顯式自動釋放你的臨時詞典:

[toolList addObject:[[[NSMutableDictionary alloc] 
    initWithObjectsAndKeys:@"Some title",@"name", 
      @"1",@"whatViewController", 
      @"",@"url", 
      @"some_icon.jpg",@"picture", 
      @"some detail text",@"detailText",nil] autorelease]]; 
+0

哇...感謝您的非常快的響應! – uncivilized 2010-11-09 14:58:16

+0

我也發現這篇文章基本上解釋了你提供的內容:http://www.memo.tv/memory_management_with_objective_c_cocoa_iphone – uncivilized 2010-11-09 14:58:40