0

I have a simple UIView child and I'm very confused as to why I'm getting this error (which causes the app to crash):

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

Here is the VideoView class:

@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

Doesn't my dealloc ensure that a notification is never sent to a deallocated instance? How else can I prevent that from happening?

iOS 9, ARC enabled

Cbas
  • 6,003
  • 11
  • 56
  • 87

1 Answers1

2

You are mixing things up. The error is not caused by the NSNotificationCenter notification. Somewhere in your code you are using key-value observing and you do not remove that observer when the object is deallocated that is why your crash occurs.

Jelly
  • 4,522
  • 6
  • 26
  • 42
  • Ahh right, adding `[self.player removeObserver:self forKeyPath:@"currentItem.status"];` solved the problem. Have to be careful about exceptions though: http://stackoverflow.com/questions/1582383/how-can-i-tell-if-an-object-has-a-key-value-observer-attached – Cbas May 03 '16 at 19:22