gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 06:21:11 27 4
gpt4 key购买 nike

对于 ARC,有时我仍然需要编写一个 -dealloc 方法来进行一些清理。在极少数情况下,我需要引用实例的属性才能正确进行清理。例如从 NSNotificationCenter 中注销给定的发送者对象,该对象由属性拥有和引用。

ARC 是在我自己的代码之后的末尾插入它的属性释放代码,还是在 -dealloc 的开头,在我自己的代码之前插入它?

如果 ARC 将在任何自定义 -dealloc 代码之前插入代码,那么这将非常危险,因为如果需要,您将无法再访问属性。

这个问题是关于 ARC 在合成的 -dealloc 中插入属性释放代码的位置,而不是关于是否实现 -dealloc。

最佳答案

我想,一切都在这里说: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.

关于iphone - ARC 是否在任何自定义 -dealloc 代码之前或之后在 -dealloc 中插入代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8964828/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com