嵌套的dispatch_async中的EXC_BAD_ACCESS与NSAutoreleasePool

发布于 2024-12-28 14:17:01 字数 1380 浏览 5 评论 0原文

我有一些类似于以下代码的代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

各空 2025-01-04 14:17:01

仪器将快速完成此工作 - 与僵尸一起运行并在停止时检查引用计数。

Instruments will make quick work of this - run with zombies and review the reference counts when it stops.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文