嵌套的dispatch_async中的EXC_BAD_ACCESS与NSAutoreleasePool
我有一些类似于以下代码的代码:
dispatch_queue_t queue = dispatch_queue_create("", 0);
dispatch_queue_t inner_queue = dispatch_queue_create("", 0);
dispatch_async(queue,
^{
NSAutoreleasePool* autoreleasePool = [[NSAutoreleasePool alloc] init];
NSArray* objects = [self.otherObject getObjectsFromSlowQuery];
[objects enumerateObjectsWithUsingBlock:^(id anObject, NSUInteger idx, BOOL *stop)
{
[anObject prepare];
dispatch_async(inner_queue,
^{
InnerObject* innerObject = anObject.innerObject;
[self.helper doSomethingExpensiveWithObject:innerObject];
});
dispatch_sync(self.syncQueue,
^{
[self insertIntoCollection:anObject];
});
}];
dispatch_release(inner_queue);
[autoreleasePool drain];
});
dispatch_release(queue);
其中 [anObject.innerObject]
是一个 nonatomic
属性。
我收到了用户的崩溃报告,当尝试访问 中的
调用。innerObject
的属性时,该报告在 objc_msgSend()
中显示 EXC_BAD_ACCESS doSomethingExpectiveWithObject:
起初我想也许 autoreleasePool
已耗尽,因此 innerObject
实例在从 doSomethingExpectiveWithObject:
返回之前被释放,但据我所知 < code>anObject 应由内部 dispatch_async
调用保留,该调用也应使 innerObject
保持活动状态。
我缺少什么?
I have some code which is similar to the following code:
dispatch_queue_t queue = dispatch_queue_create("", 0);
dispatch_queue_t inner_queue = dispatch_queue_create("", 0);
dispatch_async(queue,
^{
NSAutoreleasePool* autoreleasePool = [[NSAutoreleasePool alloc] init];
NSArray* objects = [self.otherObject getObjectsFromSlowQuery];
[objects enumerateObjectsWithUsingBlock:^(id anObject, NSUInteger idx, BOOL *stop)
{
[anObject prepare];
dispatch_async(inner_queue,
^{
InnerObject* innerObject = anObject.innerObject;
[self.helper doSomethingExpensiveWithObject:innerObject];
});
dispatch_sync(self.syncQueue,
^{
[self insertIntoCollection:anObject];
});
}];
dispatch_release(inner_queue);
[autoreleasePool drain];
});
dispatch_release(queue);
Where [anObject.innerObject]
is a nonatomic
property.
I got a crash report from a user which shows an EXC_BAD_ACCESS in objc_msgSend()
when trying to access a property of innerObject
within the doSomethingExpensiveWithObject:
call.
At first I was thinking that perhaps the autoreleasePool
was drained so the innerObject
instance was released before returning from doSomethingExpensiveWithObject:
but as far as I know anObject
should be retained by the inner dispatch_async
call which should also keep the innerObject
alive.
What am I missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
仪器将快速完成此工作 - 与僵尸一起运行并在停止时检查引用计数。
Instruments will make quick work of this - run with zombies and review the reference counts when it stops.