views:

27

answers:

1

Hey Guys, I've another problem. This time 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?

+1  A: 

Your tile object has been deallocated but it is still registered with the notificationCenter to receive notifications. Try adding a breakpoint on the tile's -dealloc method if this isn't what you are expecting.

mustISignUp
That's strange. I just tested it and you are right. The tile gets deallocated. But I don't know why. I'll look where this happens... xD
Sandro Meier
Great. That was it. In the time between I added the tile to a few other scenes. And in these scenes, the tiles get deallocated, but not removed as an observer. So the crash occured... Thank you!
Sandro Meier