这是 Cocoa 引用计数的正确总结吗?
这是我的理解:
仅保留对象(引用计数器递增):
- 当调用
NSObject
中的init
时。 - 当调用
retain
时。
仅释放对象(引用计数器递减):
- 当调用
release
时。 - 当包含该对象的自动释放池被耗尽时
需要澄清的是,自动释放不会保留,而只是将对象放入自动释放池堆栈的最顶层池中。
Here's my understanding of it:
Object are only retained (reference counter is incremented):
- when
init
fromNSObject
is called. - when
retain
is called.
Objects are only released (reference counter is decremented):
- when
release
is called. - 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不。
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.)