Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strange NSNotificationCenter crash

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?

like image 967
Sandro Meier Avatar asked Dec 01 '22 04:12

Sandro Meier


1 Answers

Had the same problem myself. Adding this to the class solved the problem

- (void) dealloc 
{

  [[NSNotificationCenter defaultCenter] removeObserver:self];

}
like image 190
Tom Tallak Solbu Avatar answered Dec 05 '22 09:12

Tom Tallak Solbu