ARC 是否在任何自定义 -dealloc 代码之前或之后在 -dealloc 中插入代码?
使用 ARC 有时我仍然需要编写一个 -dealloc 方法来进行一些清理。在某些罕见的情况下,我需要引用实例的属性来正确进行清理。例如,使用给定的发送者对象从 NSNotificationCenter 取消注册,该发送者对象由属性拥有和引用。
ARC 是否将其属性释放代码插入到我自己的代码之后的末尾,或者将其插入到 -dealloc 的开头、我自己的代码之前?
如果 ARC 在任何自定义 -dealloc 代码之前插入代码,那么这将非常危险,因为如果需要,您将无法再访问属性。
这个问题是关于 ARC 在合成的 -dealloc 中插入属性释放代码的位置,而不是关于是否实现 -dealloc。
With ARC sometimes I still need to write a -dealloc method to do some cleanup. In some rare cases I need to refer to properties of the instance to properly do cleanup. Such as unregistering from NSNotificationCenter with a given sender object, which is owned and referenced by a property.
Does ARC insert it's property release code at the end of after my own code, or does it insert this at the beginning of -dealloc, before my own code?
If ARC would be inserting code before any custom -dealloc code, then this would be very dangerous since you can't access properties anymore if needed.
This question is about where ARC inserts the property release code in a synthesized -dealloc, and not about wether or not to implement -dealloc.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为,一切都在这里说:http://clang.llvm。 org/docs/AutomaticReferenceCounting.html#misc.special_methods.dealloc
I think, that everything is said here: http://clang.llvm.org/docs/AutomaticReferenceCounting.html#misc.special_methods.dealloc
ARC 在 dealloc 链的末尾释放实例变量。换句话说,继承链中的所有 dealloc 方法都会运行,然后任何 ARC 管理的实例变量都将被释放。您可以认为它发生在
-[NSObject dealloc]
中,尽管它实际上比那更晚。所以放心; ARC 不会在您的
-dealloc
方法中释放您的属性。除非您无法再引用它们,否则 ARC 不会释放它们。ARC releases instance variables at the end of the dealloc chain. In other words, all the dealloc methods in the inheritance chain are run, and then any ARC-managed instance variables will be deallocated. You could think of it as happening in
-[NSObject dealloc]
, though it's really even later than that.So rest assured; ARC won't release your properties out from under you in your
-dealloc
method. ARC won't release them until you have no way to reference them anymore.