如果子对象在父对象的dealloc中释放,为什么子对象对父对象的弱引用为nil * [英] If a child object is released in the dealloc of a parent object, why would the child object's weak reference to the parent be nil*

查看:112
本文介绍了如果子对象在父对象的dealloc中释放,为什么子对象对父对象的弱引用为nil *的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

*因为子级在父级的 dealloc 中被释放,并不意味着父级对象仍然存在,并且该子级的弱引用回到父级应该仍然有效?

*because the child is released within the dealloc of the parent, wouldn't that imply that the parent object still exists, and that the child's weak reference back to the parent should still be valid?

我有一个父对象,一个 NSViewController 子类,它拥有另一个对象,即 NSWindowController 子类。

I have a parent object, a NSViewController subclass, which owns another object, a NSWindowController subclass.

@interface MyViewController : NSViewController
@property (strong) MyWindowController *myWindowController;

NSWindowController 子类的引用较弱到 NSViewController

@interface MyWindowController : NSWindowController
@property (weak) MyViewController *myViewController;

MyViewController 内,我已覆盖 -dealloc

-(void)dealloc
{
   [self.myWindowController close];
   self.myWindowController = nil;
}

MyWindowController内,我在 init / close 方法内添加/删除观察者。

And inside of MyWindowController, I am adding/removing observers inside of the init/close methods.

//INIT:
[self.myViewController addObserver:self forKeyPath:NSStringFromSelector(@selector(selectedObjects)) options:kObservingOptions context:nil];


//CLOSE:
[self.myViewController removeObserver:self forKeyPath:NSStringFromSelector(@selector(selectedObjects))];

现在这是令人困惑的部分。

Now here's the confusing part.

取消分配的操作顺序如下:

The order of operations on deallocation goes like this:


  • -[MyViewController dealloc]

  • -[MyWindowController关闭]

  • -[MyViewController removeObserver :MyWindowController]

  • -[MyViewController dealloc]
  • -[MyWindowController close]
  • -[MyViewController removeObserver:MyWindowController]

但是,在-[MyWindowController close] ,对 MyViewController 的弱引用是 nil 。即使最初是 MyViewController 调用 close

BUT, at the point of -[MyWindowController close], the weak reference to MyViewController is nil. Even though it was MyViewController that called close in the first place.

这会导致问题,因为没有正确删除观察者,后来引发异常,甚至崩溃。

This causes issues because the observer isn't properly removed, and later throws exceptions and sometimes even crashes.

它的外观看起来例如,在调用 -dealloc 之前,已将对 MyViewController 的保留计数减为0,因此 MyWindowController 对它的弱引用被设置为 nil 。但这对我来说没有意义,因为看起来保留计数会在之后 -dealloc 之后递减,即使那不是在这种情况下,弱引用是否为 nil

What it looks like, is that the retain count to MyViewController had already been decremented to 0 before the -dealloc is called, so MyWindowController's weak reference to it gets set to nil. But that doesn't make sense to me because it would seem like the retain count would decrement after -dealloc, and also even if that weren't the case, would the weak reference be nil?

这可能只是奇怪的交互作用w / NSViewController 拥有 NSWindowController ,但这对我来说也没有太大意义

It may be possible that this is just a weird interaction w/ NSViewController owning a NSWindowController, but that doesn't make much sense to me either

为什么弱的 @属性 == nil

推荐答案

来自Apple的在ARC上的文档


弱引用不会延长证书的有效期指向该对象的对象,当没有对该对象的强引用时,该对象自动变为nil。

A weak reference does not extend the lifetime of the object it points to, and automatically becomes nil when there are no strong references to the object.

请注意,它不会说当一个物体是释放。如果调用对象 dealloc 方法,则根据定义,所有强引用都已删除。

Note, that it doesn't say "when an object is deallocated." If an objects dealloc method is being called, then all strong references have, by definition, been removed.

,您不应在已被释放的对象上调用方法。在释放对象之前,您应该找到一种注销观察者的方法。

Furthermore, you shouldn't be calling methods on objects that have already been deallocated. You should find a way to unregister your observer before the object is deallocated.

这篇关于如果子对象在父对象的dealloc中释放,为什么子对象对父对象的弱引用为nil *的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆