2012-07-18 40 views
5

Đây là lỗi Tôi nhậnNSNotification EXC_BAD_ACCESS

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

On line này

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

Trong tập tin AsyncImageView.m.

Lỗi dừng mã nhưng nếu tôi tiếp tục trong trình gỡ rối, nó đóng băng Xcode và tắt nó đi. Làm cách nào để khắc phục sự cố này?

+0

Làm thế nào mà bạn khai báo 'userInfo'? – Kjuly

+0

'\t \t NSMutableDictionary * UserInfo = [NSMutableDictionary dictionaryWithObjectsAndKeys: \t \t \t \t \t \t \t \t \t \t hình ảnh, AsyncImageImageKey, \t \t \t \t \t \t \t \t \t \t URL, AsyncImageURLKey, \t \t \t \t \t \t \t \t \t \t nil]; ' – BigT

Trả lời

3

Hãy thử đoạn code dưới đây, nó sẽ không sao:

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

hay:

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

đó làm việc nhưng tôi cũng có một AsyncImagView.m mới và dòng trở này' [[NSNotificationCenter defaultCenter] postNotificationName: AsyncImageLoadDidFinish \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t đối tượng: _target \t \t \t \t \t \t \t \t \t \t \t \t \t \t UserInfo: [[UserInfo sao chép] autorelease]]; ' – BigT

+0

@BigT Xin lỗi, tôi không phải là rất rõ ràng về những gì bạn said..Have bạn giải quyết vấn đề này ?? – Kjuly

+0

Có, tôi có. Dòng đó đã hoạt động nhưng tôi đã cập nhật AsyncImageView.m và dòng đó đã được thay thế. Cả hai đều hoạt động. – BigT

15

Trong init bạn cần đăng ký, và trong dealloc bạn cần phải đăng ký un!

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

HOẶC

- (void)dealloc 
{ 
    [[NSNotificationCenter defaultCenter] removeObserver:self]; 
} 
Các vấn đề liên quan