奇怪的NSNotificationCenter崩溃

时间:2010-09-26 11:00:20

标签: iphone objective-c nsnotificationcenter

嘿伙计们, 我还有另外一个问题。这次是使用NSNotificationCenter。现在它崩溃了,但几天前,当我添加通知时,它运行正常。在我添加一些与之无关的代码之间......

我有大约10x10的瓷砖。每个图块在创建后立即将其自身添加为观察者:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerJumped) name:@"TestNot" object:nil];

在我的播放器课程中,每次跳转结束时,我都会使用以下代码发布通知:

if (self.postNotifications == YES) {
    //Also post the notification for all the Tiles.
    [[NSNotificationCenter defaultCenter] postNotificationName:@"TestNot" object:self];
}

如果我在图块中使用NSLog(),我可以看到大约3或4个图块接收通知。之后,应用程序崩溃了EXC_BAD_ACCESS。它说objc_msgSend() selector name: playerJumped。但我不明白为什么。我发现它适用于第一个而不是崩溃。 我的错误是什么?你能帮我么! 桑德罗

编辑:是否存在问题,因为大约100个对象收到通知?

2 个答案:

答案 0 :(得分:10)

我自己也有同样的问题。 将此添加到类中解决了问题

- (void) dealloc 
{

  [[NSNotificationCenter defaultCenter] removeObserver:self];

}

答案 1 :(得分:9)

您的磁贴对象已取消分配,但仍在通知中心注册以接收通知。如果这不是您期望的那样,请尝试在tile的-dealloc方法上添加断点。

相关问题