Post of NSNotificationCenter causing "EXC_BAD_ACCESS" exception

20,377

Solution 1

One of your subscribers has been deallocated. Make sure to call [[NSNotificationCenter defaultCenter] removeObserver:self] in your dealloc (if not sooner).

Solution 2

EXC_BAD_ACCESS can happen even after verifying dealloc exists like so:

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

The above will solve the problem most of the time, but apparently my cause was that I was indirectly adding the observer with a selector: set to nil as follows:

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

...so when I posted something with that notificationName, EXC_BAD_ACCESS occurred.

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

Solution 3

I had the same issue in Swift. The problem was the function target had a closure parameter with default value:

@objc func performFoo(completion: (() -> Void)? = nil) {
   ...
}

After I replace the closure parameter with a Notification parameter, it worked:

@objc func performFoo(notification: Notification) {
    ...
}

I had to make some refactor to make it works in a right way.

Share:
20,377
Paul Jordan
Author by

Paul Jordan

Updated on July 05, 2022

Comments

  • Paul Jordan
    Paul Jordan almost 2 years

    A UIViewController adds itself to the default center:

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

    Then a UITableView delegate NSObject posts a NSNotification:

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

    During run time it get a EXC_BAD_ACCESS exception.

    Is the defaultCenter getting released somewhere? The same concept works when I post a notification to a UIViewController from a UIViewController, but that shouldn't matter, right?

    • mostafa tourad
      mostafa tourad about 13 years
      Where exactly is it crashing?
    • nodebase
      nodebase almost 10 years
      adding [[NSNotificationCenter defaultCenter] removeObserver:self] to -(void)dealloc{} method in your Scene will probably solve this problem for you. It worked for me; I was having the same problem you were. Good luck!
  • Paul Jordan
    Paul Jordan about 13 years
    Thanks, I just realized my mistake (After looking at this and researching for four hours). The object I was attempting to reference after the call had been released. The debugger just made it look like that's where the EXC_BAD_ACCESS exception was being thrown.
  • Sven
    Sven about 13 years
    @Paul: The Zombies instrument is really helpful in debugging this kind of problem.
  • Paul Jordan
    Paul Jordan about 13 years
    @Sven Thanks, I appreciate it. I actually tried using that once, and couldn't figure out how. I added an environment variable in the project plist, but that wouldn't work.
  • jpd
    jpd over 11 years
    i'm releasing that object after notification called i got EXC_BAD_ACCESS how can i solve that?
  • junglecat
    junglecat over 10 years
    EXC_BAD_ACCESS is not an exception, it's invalid memory access
  • Billy Gray
    Billy Gray over 9 years
    Yup, just found this myself, I had put NULL as the selector intending it to be temporary, like oh let me go create that method, then I forgot to go update the selector param later (DOH) and yep, EXC_BAD_ACCESS result.