NSNotificationCentre Crash

cocoa-touch, ios, nsnotificationcenter, uikit

Solution

This is because when you get the notification, if you havent removed your class as an observer, it still tries calling the method. However, since the object has been completely deallocated and destroyed, you get an EXC_BAD_ACCESS.

Problem

In my Application I listen for keyboard notifications: ``` [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow) name:UIKeyboardWillShowNotification object:nil]; ``` I have just removed a bug which caused a crash in my app, I have a modal view with a UI (which gets destroyed and recreated each time it's presented. I was getting a crash the second time I went to use it before I added this line of code: ``` [[NSNotificationCenter defaultCenter] removeObserver:self]; ``` Any one know why not removing observer for a dealloced object caused a crash?

Original source