NSNotification EXC_BAD_ACCESS

时间:2012-07-18 14:13:16

标签: xcode exc-bad-access nsnotificationcenter

这是我得到的错误

Thread 1:EXC_BAD_ACCESS (code=2, address=0xb7ffffc)

在这一行

[[NSNotificationCenter defaultCenter] postNotificationName:AsyncImageLoadDidFinish
                                                        object:target
                                                      userInfo:[[userInfo copy] autorelease]];

在AsyncImageView.m文件中。

错误会停止代码但如果我继续在调试器中冻结Xcode并将其关闭。我该如何解决这个问题?

2 个答案:

答案 0 :(得分:15)

在init中你需要注册,在dealloc中你需要注册!

-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:AsyncImageLoadDidFinish  object:nil];

OR

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

答案 1 :(得分:3)

尝试下面的代码,应该没问题:

NSDictionary * userInfo = [NSDictionary dictionaryWithObjectsAndKeys:..., nil];
[[NSNotificationCenter defaultCenter] postNotificationName:AsyncImageLoadDidFinish
                                                    object:target
                                                  userInfo:userInfo];

或:

NSDictionary * userInfo = [[NSDictionary alloc] initWithObjectsAndKeys:..., nil];
[[NSNotificationCenter defaultCenter] postNotificationName:AsyncImageLoadDidFinish
                                                    object:target
                                                  userInfo:userInfo];
[userInfo release];