ARC seems very nice but there are one or two edge cases that the typical naming conventions/rules don’t make clear to me. Look at the following category implementation around NSThread:
@interface NSThread (BlockAdditions)
- (void)performBlock:(dispatch_block_t)block;
@end
@implementation NSThread (BlockAdditions)
- (void)internal_performBlock:(dispatch_block_t)block
{
block();
}
- (void)performBlock:(dispatch_block_t)block
{
[self performSelector:@selector(internal_performBlock:)
onThread:self
withObject:[block copy]
waitUntilDone:NO];
}
My question is: does block leak after calling -copy? How would the compiler know when to release the block? Instruments does not detect a leak but that does not convince me, given what I know about ARC, that this case is handled correctly. Thanks for any info!
That would leak in retain/release, but should not leak in ARC.
The compiler sees the
-copyand that implies that a-releaseis needed. If you look at the generated assembly, that should be exactly what you see.(well, exactly what you see once you wade through the assembly, which isn’t exactly straightforward.)
Note that you can simplify the assembly by just compiling the
[block copy];.