observeValueForKeyPath:发送到已释放实例的消息



我有一个简单的UIView子项,我很困惑为什么会出现这个错误(导致应用程序崩溃):

[VideoView observeValueForKeyPath:ofObject:change:context:]: message sent to deallocated instance 0x13009b670

这是VideoView类:

@implementation VideoView
- (id)init {
    self = [super init];
    if (self) {
        [[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(stopVideo)
                                             name:UIApplicationDidEnterBackgroundNotification
                                           object:nil];
    }
    return self;
}
- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
//.. stopVideo method
@end

我的dealloc是否确保从不向已释放的实例发送通知?否则我该如何防止这种情况发生?

iOS 9,启用ARC的

你把事情搞混了。该错误不是由NSNotificationCenter通知引起的。在代码的某个地方,您正在使用键值观察,并且在对象被释放时没有删除该观察者,这就是崩溃发生的原因。

最新更新