代码之家  ›  专栏  ›  技术社区  ›  Sandro Meier

奇怪的NSNotificationCenter崩溃

  •  4
  • Sandro Meier  · 技术社区  · 15 年前

    我有大约10x10块瓷砖。每个磁贴一经创建即添加自身作为观察者:

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

    在我的player类中,每次跳转结束时,我都会发布一个带有以下代码的通知:

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

    如果我在tile中使用NSLog(),我可以看到大约3或4个tile收到通知。在那之后,应用程序崩溃了,出现了EXC\u BAD\u访问。上面写着 objc_msgSend() selector name: playerJumped . 但我不明白为什么。我看到它和第一个一起工作,而不是崩溃。 桑德罗

    编辑:是否有问题,因为通知是由大约100个对象接收的?

    2 回复  |  直到 5 年前
        1
  •  9
  •   hooleyhoop    15 年前

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

        2
  •  10
  •   Tom Tallak Solbu    13 年前

    我自己也有同样的问题。

    - (void) dealloc 
    {
    
      [[NSNotificationCenter defaultCenter] removeObserver:self];
    
    }
    
    推荐文章