Do I need to Block_release dispatch_block_t which passed to the dispatch_barrier_async when no ARC ?

Do I need to release the block which passed to the dispatchbarrierasync when no ARC ? I noticed "The barrier block to submit to the target dispatch queue. This block is copied and retained until it finishes executing, at which point it is released." in dispatchbarrierasync.
Code Block
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?
}
}

Thanks!

Do I need to release the block which passed to the
dispatch_barrier_async when no ARC ?

Yes, although not because of the dispatch_barrier_async call which, as you noted, is documented to manage its own copy. Rather, this is the result of your call to dispatch_block_create. It is annotated with DISPATCH_RETURNS_RETAINED_BLOCK, which means that the caller is responsible for releasing the result. So the code on line 13 is correct.

One of the nice things about Cocoa’s manual retain-release system is that it works locally. You can look at any function and see whether its memory management is correct solely based on the contents of that function. So, for example, you don’t have to worry about the behaviour of dispatch_barrier_async and dispatch_block_wait — if they need to copy the block, they will — but instead focus on your creates, copies, retains, releases, and autoreleases.

ps Testing work for NULL is kinda pointless here. If dispatch_block_create fails when you call it this way — that is, with parameters that you know are valid — then your process isn’t going to run for long anyway.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@apple.com"
Do I need to Block_release dispatch_block_t which passed to the dispatch_barrier_async when no ARC ?
 
 
Q