移除 KVO 观察器时 APP 崩溃

APP crashes when remove KVO observer

我在控制器中有一个滚动视图。滚动视图有一个子视图。子视图同时是滚动视图的观察者。当调用子视图的 willMoveToSuperview: 时,我删除了观察者。但是当控制器关闭时,应用程序崩溃了。以下是示例代码:

@interface MyView : UIView

@property (nonatomic, weak) UIScrollView *scrollView;

@end

@implementation MyView

- (instancetype)initWithFrame:(CGRect)frame scrollView:(UIScrollView *)scrollView {
    self = [super initWithFrame:frame];
    if (self) {
        self.scrollView = scrollView;
        [scrollView addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew context:nil];
    }
    return self;
}

- (void)willMoveToSuperview:(UIView *)newSuperview {
    [super willMoveToSuperview:newSuperview];

    if (!newSuperview) {
        [self.scrollView removeObserver:self forKeyPath:@"contentOffset"];
        self.scrollView = nil;
    }
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {
}

@end

@interface SecondViewController ()

@end

@implementation SecondViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    scrollView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:scrollView];

    MyView *view = [[MyView alloc] initWithFrame:CGRectMake(100, 200, 100, 100) scrollView:scrollView];
    [scrollView addSubview:view];
}

@end

当我在 willMoveToSuperview 中打印 self.scrollView 时,它显示为空。当我将 MyView 中的 属性 scrollView 更改为 unsafe_unretained 时,应用程序不会崩溃。 所以我很困惑。为什么不弱 scrollView 工作。当 scrollView 为 unsafe_unretained 时,我正在读取悬挂指针吗?对于这种情况有更好的解决方案吗?

这里的问题是在 willMoveToSuperview 被调用时 scrollView weak 指针已经 nil(释放)。 但它认为 scrollView 没有完全释放(内存未释放),这就是为什么当你使用 unsafe_unretained 引用删除观察者时它以某种方式工作。但它是一个悬空指针引用,你不应该依赖它。