This question already has an answer here:
I have reqest with block. But the compiler issues a warning
"Capturing 'self' strongly in this block is likely to lead to a retain cycle"
__weak typeof(self) weakSelf = self;
[generalInstaImage setImageWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:data[@"images"][@"low_resolution"][@"url"]]] placeholderImage:[UIImage imageNamed:@"Default"] success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
NSLog(@"success");
[generalInstaImage setImage: image];
[weakSelf saveImage:generalInstaImage.image withName:data[@"id"]];
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
NSLog(@"fail");
}];
I try example write like weakSelf.generalInstaImage
, but then the compiler generates an error and do not compile.
Consider this warning:
When you receive the above warning, you should review your block for:
self
; orself
caused by referencing any instance variables.Let's imagine that we have some simple class property that was a block (this will experience the same "retain cycle" warnings as your question, but will keep my examples a little simpler):
And let's assume we had some other class property we wanted to use inside our block:
If you reference
self
within the block (in my example below, in process of accessing this property), you will obviously receive that warning about the retain cycle risk:That is remedied via the pattern you suggested, namely:
Less obvious, you will also receive the "retain cycle" warning if you reference an instance variable of the class inside the block, for example:
This is because the
_someString
instance variable carries an implicit reference toself
, and is actually equivalent to:You might be inclined to try to adopt weak self pattern here, too, but you can't. If you attempt the
weakSelf->_someString
syntax pattern, the compiler will warn you about this:You therefore resolve this by using the
weakSelf
pattern, but also create a localstrong
variable within the block and use that to dereference the instance variable:As an aside, this creation of a local
strong
reference,strongSelf
, inside the block has other advantages, too, namely that if the completion block is running asynchronously on a different thread, you don't have to worry aboutself
being deallocated while the block is executing, resulting in unintended consequences.This
weakSelf
/strongSelf
pattern is very useful when dealing with block properties and you want to prevent retain cycles (aka strong reference cycles), but at the same time ensuring thatself
cannot be deallocated in the middle of the execution of the completion block.FYI, Apple discusses this pattern in the "non-trivial cycles" discussion further down in the Use Lifetime Qualifiers to Avoid Strong Reference Cycles section of the Transitioning to ARC Release Notes.
You report that you received some "error" when you referenced
weakSelf.generalInstaImage
in your example. This is the correct way to resolve this "retain cycle" warning, so if you received some warning, you should share that with us, as well as show us how you declared the property.Use
__unsafe_unretained typeof(self) weakSelf = self