I have a problem with NSNotificationCenter. Now it crashes, but a few days ago, when I added the Notification, it worked properly. In the time between I added some code that has nothing to do with that.
I have about 10x10 tiles. Each tile adds itself as an observer as soon as it is created:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerJumped) name:@"TestNot" object:nil];
And in my player class, every time a jump ended, I post a Notification with the following code:
if (self.postNotifications == YES) {
//Also post the notification for all the Tiles.
[[NSNotificationCenter defaultCenter] postNotificationName:@"TestNot" object:self];
}
If I use NSLog() in the tiles, I can see that about 3 or 4 tiles receive the notification. And after that, the application crashes with a EXC_BAD_ACCESS. It says objc_msgSend() selector name: playerJumped
. But I don't get why. I see that it works with the first one than it crashes.
What's my error here? Can you please help me!
Sandro
EDIT: Is there a Problem, because the Notification is received by about 100 objects?
Had the same problem myself. Adding this to the class solved the problem
- (void) dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With