如何dealloc NSMutableDictionary

时间:2010-11-09 13:30:58

标签: objective-c memory-leaks nsmutablearray alloc

我从viewDidLoad调用createTableData。我不明白的是我正在为NSMutableDictionary做一个alloc,但我不明白为什么该对象不会从内存中释放 - 尽管发布了。 我确实看到内存泄漏,泄漏似乎指向这部分代码。 有人能指出我的网址,我可以阅读/理解我应该做什么而不是我在做什么?我似乎无法看到我在这里出错的地方。

- (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 个答案:

答案 0 :(得分: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]];