Strange accident NSNotificationCenter

Hey guys, I have another problem. This time with NSNotificationCenter. Now it crashes, but a few days ago, when I added a notification, it worked correctly. Between me, I added code that has nothing to do with this ...

I have about 10x10 plates. Each fragment 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 the jump ends, I send 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 a tile, I see that 3 or 4 tiles receive a notification. And after that, the application crashes with EXC_BAD_ACCESS. It says objc_msgSend() selector name: playerJumped . But I don’t understand why. I see that it works with the first, than failure. What is my mistake here? Could you help me! Sandro

EDIT: is there a problem because the notification was received by about 100 objects?

+4
source share
2 answers

Your tile object has been released, but it is still registered in the notification to receive notifications. Try adding a breakpoint in the tile -dealloc method if this is not what you expect.

+9
source

I had the same problem. Adding this class solved the problem.

 - (void) dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; } 
+10
source

All Articles