了解 NSDictionary 版本的内存管理

发布于 2024-10-17 00:43:18 字数 863 浏览 5 评论 0原文

iOS 内存管理对我来说仍然是一个奇怪的东西,但这对我来说也是最有趣的方面,所以我在这里寻求一些关于我的代码的帮助。

我尝试实例化一个 NSDictionary 对象,我使用它然后我想释放,但我收到一个对象释放错误,这是代码:

if ([jsonArray count] > 0) {        
    NSDictionary *commentDictionary = [[NSDictionary alloc]init];
    int i;
    for (i = 0; i < [jsonArray count]; i++) {
        commentDictionary = [jsonArray objectAtIndex:i];
        NSLog(@"debugging message here"]);
        commentLabel.text = [commentDictionary objectForKey:@"commentText"];
        //[commentDictionary retain];
    }
    //[commentDictionary release];
    commentDictionary = nil;
    NSLog(@"NSDictionary retainCount = %d",[commentDictionary retainCount]);
}

没什么特别的,我从数组填充字典,在我的代码中你可以看到我尝试过发布,但我因为错误而注释掉了。 为什么我无法发布词典?

此外,将 NSDictionary 设置为 nil 并在保留计数和释放中返回零(这应该使计数为-1)之间有什么区别?

我提前非常感谢您在此主题中提供的任何帮助。

法布里奇奥

iOS memory management is still something weird to understand to me but that's the most interesting aspect to me also, so I'm asking for some help here with my code.

I try to instantiate a NSDictionary object, I use it then I'd like to release but I got an object released error, this is the code:

if ([jsonArray count] > 0) {        
    NSDictionary *commentDictionary = [[NSDictionary alloc]init];
    int i;
    for (i = 0; i < [jsonArray count]; i++) {
        commentDictionary = [jsonArray objectAtIndex:i];
        NSLog(@"debugging message here"]);
        commentLabel.text = [commentDictionary objectForKey:@"commentText"];
        //[commentDictionary retain];
    }
    //[commentDictionary release];
    commentDictionary = nil;
    NSLog(@"NSDictionary retainCount = %d",[commentDictionary retainCount]);
}

Nothing special, I fill a dictionary from an array, in my code you can see I tried to release but than I commented out because of the error.
Why I can't release the dictionary?

Besides what's the difference between setting NSDictionary to nil that returns zero in retainCount and release (that should make the count -1)?

I thank you very much in advance for any kind of help in this topic.

Fabrizio

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

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

发布评论

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

评论(2

国粹 2024-10-24 00:43:18

不要调用retainCount

retainCount 对于尝试找出内存管理来说是一个可怕的方法。对象的绝对保留计数很少令人感兴趣,并且由于实现细节而常常难以理解。

阅读文档。这非常简单。

现在,看看你的代码。

  • 第一行中的 commentDictionary 的 alloc/init 赋值是不需要的,并且该对象将在 for() 循环。

  • 您可以使用 for(commentDictionary in jsonArray) {...} 而不是使用 for(;;)

  • 没有理由在该代码中保留或释放 commentDictionary ;从数组中检索的对象将在该方法的整个范围内保持有效。

  • Objective-C 是一种“零吃消息”的语言。当您调用 nil 上的方法时,该调用在几乎所有情况下都会返回 0。


哦,还有西里尔说的话。静态分析器是一个很棒的工具!

Do not call retainCount

retainCount is a horrible method to use for trying to figure out memory management. The absolute retain count of an object is rarely interesting and often will be unfathomable due to implementation details.

Read the documentation. It is pretty straightforward.

Now, to your code.

  • the alloc/init assignment to commentDictionary in your first line isn't needed and that object will be leaked on the assignment that is the first line in the for() loop.

  • instead of using for(;;), you could use for(commentDictionary in jsonArray) {...}

  • there is no reason to retain or release commentDictionary in that code; the object retrieved from the array will remain valid throughout the scope of that method.

  • Objective-C is a "nil eats messages" language. When you call a method on nil, that call will return 0 in almost all cases.


Oh, and what Cyril said. The static analyzer is a wonderful tool!

谈情不如逗狗 2024-10-24 00:43:18

我建议您对该代码运行静态分析器:我在内存管理方面犯的很多错误都是通过遵循小蓝色箭头的步骤来解释的
描述。

这是一个非常有用/很酷/宽容的工具,可以发现自己的错误并了解正在发生的事情。构建菜单中的构建和分析。

PS:retainCount经常出错;

I advise you to run the static analyzer on that code: a lot of the mistakes that I do regarding memory management are explained by following the steps that the little blue arrows
describe.

That's a very useful/cool/forgiving tool to discover your own mistakes and understand what's happening. Build and Analyze in the build menu.

PS: retainCount is often wrong;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文