我需要我的类的实例本身的观察属性,所以我设置了观察:
[self addObserver:self forKeyPath:@"myProperty" options:NSKeyValueObservingOptionNew context:nil]
在 dealloc 中只需检查并删除观察者:
- (void)dealloc
{
if ([self observationInfo])
{
[self removeObserver:self];
}
}
该项目在 ARC 下,在 iOS 6 SDK 之前一切正常。但是使用 iOS 6 SDK,在使用 EX_BAD_ACCESS(释放消息发送到已释放的实例)observationInfo
之后,对象和应用程序崩溃的增加会增加。dealloc
这段代码有什么问题?是苹果的BUG还是我的?
更新 有堆栈跟踪:
thread #1: tid = 0x1f03, 0x016b60ab libobjc.A.dylib`objc_release + 27, stop reason = EXC_BAD_ACCESS (code=1, address=0xff000002)
frame #0: 0x016b60ab libobjc.A.dylib`objc_release + 27
frame #1: 0x016b6bd9 libobjc.A.dylib`(anonymous namespace)::AutoreleasePoolPage::pop(void*) + 555
frame #2: 0x02538468 CoreFoundation`_CFAutoreleasePoolPop + 24
frame #3: 0x0253cafd CoreFoundation`__CFRunLoopRun + 1933
frame #4: 0x0253bf44 CoreFoundation`CFRunLoopRunSpecific + 276
frame #5: 0x0253be1b CoreFoundation`CFRunLoopRunInMode + 123
frame #6: 0x024f07e3 GraphicsServices`GSEventRunModal + 88
frame #7: 0x024f0668 GraphicsServices`GSEventRun + 104
frame #8: 0x0022e65c UIKit`UIApplicationMain + 1211
frame #9: 0x00001efd MyProjects`main + 141 at main.m:16