Post of NSNotificationCenter causing "EXC_BAD_ACCESS" exception
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.
Paul Jordan
Updated on July 05, 2022Comments
-
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 aNSNotification
:[[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 about 13 yearsWhere exactly is it crashing?
-
nodebase almost 10 yearsadding
[[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 about 13 yearsThanks, 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 about 13 years@Paul: The Zombies instrument is really helpful in debugging this kind of problem.
-
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 over 11 yearsi'm releasing that object after notification called i got EXC_BAD_ACCESS how can i solve that?
-
junglecat over 10 yearsEXC_BAD_ACCESS is not an exception, it's invalid memory access
-
Billy Gray over 9 yearsYup, 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.