An NSNotificationCenter message that throws an EXC_BAD_ACCESS exception

A UIViewController adds itself to the center by default:

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(editFood) name:@"editFood" object:nil]; 

Then a UITableView delegate of NSObject sends a NSNotification :

 [[NSNotificationCenter defaultCenter] postNotificationName:@"editFood" object:self]; 

At run time, it gets an EXC_BAD_ACCESS exception.

Is defaultCenter anywhere? The same concept works when I send a notification to a UIViewController from a UIViewController, but that doesn't matter, right?

+62
ios objective-c iphone nsnotificationcenter exc-bad-access
Apr 14 '11 at 19:41
source share
2 answers

One of your followers has been released. Be sure to call [[NSNotificationCenter defaultCenter] removeObserver:self] in your dealloc (if not earlier).

+127
Apr 14 '11 at 19:44
source share

EXC_BAD_ACCESS may occur even after verifying that dealloc exists as follows:

 - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self] } 

The above problem will be solved most of the time, but apparently my reason was because I indirectly added an observer with selector: to the nil value as follows:

 [NSNotificationCenter.defaultCenter addObserver:self selector:nil name:notificationName object:nil]; 

... so when I sent something with this notificationName , EXC_BAD_ACCESS .

The solution was to send a selector that actually points to something.

+9
Oct 13 '14 at 6:10
source share



All Articles