NSNotificationCenter and NSdistributedNotificationCenter no longer send notifications to registered observers that may be deallocated. If the observer is able to be stored as a zeroing-weak reference the underlying storage stores the observer as a zeroing weak reference. Alternatively,if the object cannot be stored weakly (because it has a custom retain/release mechanism that would prevent the runtime from being able to store the object weakly) the object is stored as a non-weak zeroing reference. This means that observers are not required to un-register in their deallocation method. [emphasis mine]
这对我来说没有意义.如果它是一个非弱参考,那么它不是一个强大的参考吗?因此,NSNotificationCenter仍然是所有者,因此该对象不会解除分配(直到手动取消注册),因此在此上下文中说它是“归零”是没有意义的.
如果这是指某种__unsafe_unretained引用,那么问题是……那么NSNotificationCenter如何避免传播僵尸?
解决方法 答案就在于Objective-c运行时,以及__weak变量实际如何工作.为了解释一下,让我们看一下objc_weak.mm:ID weak_read_no_lock(weak_table_t *weak_table,ID *referrer_ID) { ... if (! referent->ISA()->hasCustomrR()) { if (! referent->roottryRetain()) { return nil; } } else { BOol (*tryRetain)(objc_object *,SEL) = (BOol(*)(objc_object *,SEL)) object_getmethodImplementation((ID)referent,SEL_retainWeakReference); if ((IMP)tryRetain == _objc_msgForward) { return nil; } if (! (*tryRetain)(referent,SEL_retainWeakReference)) { return nil; } } return (ID)referent;}
正如您所看到的,当对象使用自定义-retain和-release方法时,并不能保证它们完全支持弱引用(还要注意,对于对象的弱引用,您可以使用完全不同的对象,尽管这是一个主题另一次).
这是因为objc_destructInstance清除了弱引用,后者调用objc_clearDeallocating,调用weak_clear_no_lock.
现在,objc_destructInstance不需要被自定义对象实现调用,尽管大多数对象都会调用它.
因此,运行时允许您实现方法-allowsWeakReference(和retainWeakReference)来禁用对对象的弱引用,在这种情况下,它很可能通过对象上的swizzling -dealloc归零.当然,这是所有实现细节,因此NSNotificationCenter可以拥有自己创新的做事方式,但这是我最好的猜测而不试图反汇编NSNotificationCenter.
总结以上是内存溢出为你收集整理的objective-c – 什么是“非弱归零参考”全部内容,希望文章能够帮你解决objective-c – 什么是“非弱归零参考”所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)