0

我从 viewDidLoad 调用 createTableData。我不明白的是我正在为 NSMutableDictionary 做一个分配,但我不明白为什么该对象没有从内存中释放——尽管释放了。我确实看到了内存泄漏,并且 Leaks 似乎指向这部分代码。有人可以指向我可能能够阅读/理解我应该做什么和我正在做什么的网址吗?我似乎看不出我在哪里出错了。

- (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];
}
4

1 回答 1

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]];
于 2010-11-09T13:33:46.377 回答