ARC 是否在任何自定义 -dealloc 代码之前或之后在 -dealloc 中插入代码?

发布于 2024-12-28 12:16:36 字数 350 浏览 3 评论 0原文

使用 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 技术交流群。

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

发布评论

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

评论(2

好倦 2025-01-04 12:16:36

我认为,一切都在这里说:http://clang.llvm。 org/docs/AutomaticReferenceCounting.html#misc.special_methods.dealloc

7.1.2。解除分配

如果程序包含消息发送或选择器 dealloc 的 @selector 表达式,则该程序是格式错误的。

理由:没有正当理由直接调用 dealloc。

类可以为名为 dealloc 的实例方法提供方法定义。该方法将在对象最终释放之后但在其被释放或其任何实例变量被销毁之前被调用。当方法返回时,将自动调用超类的 dealloc 实现。

基本原理:即使 ARC 自动销毁实例变量,仍然有编写 dealloc 方法的正当理由,例如释放不可保留的资源。在这样的方法中无法调用 [super dealloc] 几乎总是一个错误。有时,对象只是试图防止自身被破坏,但 dealloc 对于对象提出此类反对来说确实太晚了。更合理的是,一个对象可能已经被池分配,并且不应该用 free 来释放;目前,这只能通过 ARC 外部的 dealloc 实现来支持。这样的实现必须非常小心地完成 NSObject 的 dealloc 所要做的所有其他工作,这超出了本文档描述的范围。

I think, that everything is said here: http://clang.llvm.org/docs/AutomaticReferenceCounting.html#misc.special_methods.dealloc

7.1.2. dealloc

A program is ill-formed if it contains a message send or @selector expression for the selector dealloc.

Rationale: there are no legitimate reasons to call dealloc directly.

A class may provide a method definition for an instance method named dealloc. This method will be called after the final release of the object but before it is deallocated or any of its instance variables are destroyed. The superclass's implementation of dealloc will be called automatically when the method returns.

Rationale: even though ARC destroys instance variables automatically, there are still legitimate reasons to write a dealloc method, such as freeing non-retainable resources. Failing to call [super dealloc] in such a method is nearly always a bug. Sometimes, the object is simply trying to prevent itself from being destroyed, but dealloc is really far too late for the object to be raising such objections. Somewhat more legitimately, an object may have been pool-allocated and should not be deallocated with free; for now, this can only be supported with a dealloc implementation outside of ARC. Such an implementation must be very careful to do all the other work that NSObject's dealloc would, which is outside the scope of this document to describe.

(り薆情海 2025-01-04 12:16:36

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.

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