如何释放 NSMutableDictionary

发布于 2024-10-01 12:29:43 字数 707 浏览 5 评论 0原文

我从 viewDidLoad 调用 createTableData。我不明白的是我正在为 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];
}

I call createTableData from viewDidLoad. What I don't understand is I'm doing an alloc for a NSMutableDictionary but I don't understand why that object is not released from memory-despite the release.
I do see memory leaks and Leaks seems to point at this section of code.
Can someone point me to a url where I might be able to read/understand what I should be doing versus what I am doing? I just can't seem to see where I've gone wrong here.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

紅太極 2024-10-08 12:29:43
 [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]];
 [toolList addObject:[[NSMutableDictionary alloc]
     initWithObjectsAndKeys:@"Some title",@"name",
          @"1",@"whatViewController",
          @"",@"url",
          @"some_icon.jpg",@"picture",
          @"some detail text",@"detailText",nil]];

In this line your add NSMutableDictionary object to array and not releasing it. Correct code would be (using class method that already returns autoreleased object):

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

or explicitly autorelease your temporary dictionary:

[toolList addObject:[[[NSMutableDictionary alloc]
     initWithObjectsAndKeys:@"Some title",@"name",
          @"1",@"whatViewController",
          @"",@"url",
          @"some_icon.jpg",@"picture",
          @"some detail text",@"detailText",nil] autorelease]];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文