【问题标题】:how to dealloc NSMutableDictionary如何释放 NSMutableDictionary
【发布时间】:2010-11-09 13:30:57
【问题描述】:

我从 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];
}

【问题讨论】:

    标签: objective-c memory-leaks nsmutablearray alloc


    【解决方案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]];
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多