在使用异步调度时,我遇到了一个非常奇怪的错误访问错误。我设法将其简化为程序中的这段代码。
-(void)buttonTapped:(id)sender {
__block NSArray*foo = nil;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
//Foo was initially declared here but then moved it outside.
foo = [self someMethod];
[foo retain]; // bad access here. Why ?
dispatch_async(dispatch_get_main_queue(),0) {
// doesnt matter what happens here
}); });
}
-(id)someMethod
{
return [self secondMethod];
}
-(id)secondMethod
{
// was initially returning an autoreleased object from here. Changed it
// to eliminate that as source of the error.
id newThing = [[NSObject alloc] init];
return newThing;
}
代码最初看起来并不像这样,但现在就是这样。包括分配一个虚拟的 NSObject 。
foo 如何在调度 async 内的调用之间被释放?我不明白这怎么可能。我知道很难就此提出建议,但任何调试建议都会有所帮助。我尝试打开 NSZombies,但我没有得到任何 Zombies。