I
I
Ivan Gaidamakin2016-03-29 14:20:25
Objective-C
Ivan Gaidamakin, 2016-03-29 14:20:25

What does Capturing 'self' strongly in this block is likely to lead to a retain cycle mean?

Good afternoon!
The compiler issues the following warning:
Capturing 'self' strongly in this block is likely to lead to a retain
cycle


_autocompleteViewDestination.didAutocompleteWith = ^(id item) {
NSLog(@"Autocompleted with: %@", item.completionText);
[self.session saveDestinationPointWith:item.completionText andLocation:item.location]; <<<
};

Actually, I would like to know what this means and how to deal with it correctly with ARC enabled.
Thanks in advance for your reply!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
ManWithBear, 2016-03-29
@MeGaPk

The compiler tells you that you have a link to the block, and the block has a link to you. As a result, APC will not be able to delete these two objects and they will be forever in memory. Which eventually leads to a memory leak.
You can fight:
1) Removing the link to the block if it is not critical, or making it weak
2) Pass yourself to the block by a weak link:

__weak MyClass *me = self;
... = ^() {
[me.session ...];
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question