这是 Cocoa 引用计数的正确总结吗?

发布于 2024-08-23 13:33:46 字数 327 浏览 5 评论 0原文

这是我的理解:

保留对象(引用计数器递增):

  1. 当调用 NSObject 中的 init 时。
  2. 当调用 retain 时。

释放对象(引用计数器递减):

  1. 当调用 release 时。
  2. 当包含该对象的自动释放池被耗尽时

需要澄清的是,自动释放不会保留,而只是将对象放入自动释放池堆栈的最顶层池中。

Here's my understanding of it:

Object are only retained (reference counter is incremented):

  1. when init from NSObject is called.
  2. when retain is called.

Objects are only released (reference counter is decremented):

  1. when release is called.
  2. when an autorelease pool containing the object is drained

And to clarify, autorelease does not retain, but just places the object into the top most pool on the autorelease pool stack.

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

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

发布评论

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

评论(1

ら栖息 2024-08-30 13:33:46

不。

init 不保留,并且有多种您没有提到的方法。 Cocoa 内存管理编程指南

(我知道有两个例外:NSPropertyListSerialization 方法通过引用提供保留的错误字符串。)

No.

init does not retain, and there are various methods you didn’t mention which do. There is a simple (and also correct) summary in Memory Management Programming Guide for Cocoa.

(There are two exceptions that I’m aware of: the NSPropertyListSerialization methods which provide a retained error string by reference.)

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