当没有 ARC 时,我是否需要 Block_release dispatch_block_t 在 dispatch_barrier_async 中

Do I need to Block_release dispatch_block_t which is in the dispatch_barrier_async when no ARC

当没有 ARC 时,我是否需要 Block_release dispatch_block_t dispatch_barrier_async 中的 dispatch_barrier_async? 我注意到“提交到目标调度队列的屏障块。复制并保留该块,直到它完成执行,此时它被释放。”在 dispatch_barrier_async.

dispatch_block_t work = dispatch_block_create(0, ^{
    //...
});

if (work) {
    dispatch_barrier_async(_dispatchQueue, work);
    auto res = dispatch_block_wait(work, timeout);
    if (res) {
        // timeout, then cancel, I should release work here?
        dispatch_block_cancel(work);
    }

    Block_release(work); // do I need to release work when no ARC? the dispatch_barrier_async would release it if it's executed?
}

不使用ARC时,是的,你必须释放它。按 command+shift+o(字母“o”,不为零)并搜索 dispatch_block_create 跳转到 headers(或 control-单击代码中的 dispatch_block_create 并选择“跳转到定义”),它说:

When not building with Objective-C ARC, must be released with a -[release] message or the Block_release() function.

话虽如此,您已经发布了下面的 work,因此您当然不必在 if 声明中再次发布它。


不幸的是,静态分析器(shift+command+b),这是通常在这类手动引用计数问题上表现出色,似乎无法识别可能泄露的 dispatch_block_t 个实例。尽管如此,我仍会确保您从静态分析器获得健康证明。