I'm working on a Objective-C project and we have ARC enabled. I'm currently dealing with two separate classes, let's call them A
and B
.
A
has a strong reference to B
and B
has a block property with the copy
attribute.
@interface A {
B *b;
...
}
@end
@interface B {
...
}
@property (copy) void (^myBlock)();
@end
Now, if inside a method owned by A
I try to assign to myBlock
:
b.myBlock = ^{ [self doSomething] };
The compiler (correctly) complains:
Capturing 'self' strongly in this block is likely to lead to a retain cycle.
I understand this behaviour and how to work around it, based on the answers to this question. (Basically, a cycle is formed because once the block is copied it will contain a strong reference to A
, which has a strong reference to B
, which in turn has a strong reference to the block itself.)
Now, if instead of directly assigning the block to the property I pass it to a method of B:
[b takeMyBlock:^{ [self doSomething] }];
And inside that method I do the assignment, the compiler will no longer complain.
My question is: Will that create a memory leak? If so, how to avoid it? Is it possible to annotate the method argument somehow so that the compiler knows the argument is going to be copied?
Thanks in advance.