Saving a loop occurs when two objects store a strong link to each other. The simplest case is object a , which stores a strong reference to object b and b , doing the opposite [1]. Saving loops is a problem in Objective-C because they make ARC believe that these objects are always used, even if these objects are not referenced anywhere.
Let's look at a few examples. You have a z object that selects a and b , uses them, and then removes them. If a and b created the save cycle between themselves in the first place, a and b will not be released. If you do this several times, you will seriously leak the memory.
Another real-life example of a save loop is if a highlights and strongly refers to an object b , but you also keep a strong link from b to a (many smaller objects in the object graph may need to access their parents).
The most common solutions in these cases would be to make sure that the objects contained in them have only weak references to its containing objects, and also make sure that the twin objects do not contain strong references to each other.
Another solution (usually less elegant, but perhaps appropriate in some situations) might have some kind of custom cleanup method in a that references b . That way, b will be freed up when calling cleanup (unless b is mentioned elsewhere). This is cumbersome because you cannot do this from a dealloc (it is never called if there is a save cycle), and because you must remember to call cleanup at the appropriate times.
- Note that save loops are also transitive (for example, the object
a strongly references b , which strongly refers to c , which strongly refers to a ).
With all this, it says: managing memory blocks is pretty hard to understand.
In the first example, you can create a temporary save loop (and only if your self object stores a strong reference to someObject ). This time save cycle leaves when the block completes execution and is freed.
At runtime, self will again retain a reference to someObject , someObject to block and block to self . But again, this is temporary because the block is not permanently stored somewhere (unless the [someObject successBlock:failure:] implementation does this, but it is not often found for completion blocks).
So the save loop is not a problem in your first example.
As a rule, saving loops inside blocks is a problem only if some object stores the block rather than executing it directly. Then itβs easy to see that self refers strongly to block , and block has a strong reference to self . Note that accessing any ivar from the inside automatically generates a strong self reference in this block.
Equivalent to the fact that the contained object has no special relation to its container, uses __weak SelfClass *weakSelf = self to access both methods and ivars (it is better if you access ivars through accessors, as when using properties). Your block reference to self will be weak (this is not a copy , it is a weak link ), and this will allow self free itself if it stops linking strongly.
It can be argued that in practice it is recommended that you always use weakSelf inside all blocks, stored or not, just in case. I wonder why Apple did not do this by default. Doing this usually does nothing harmful to the block code, even if not actually used.
__block rarely used for variables that point to objects, since Objective-C does not ensure the immutability of such objects.
If you have a pointer to an object, you can call its methods, and these methods can change it, with or without __block . __block bigger (only?) useful for variables of basic types (int, float, etc.). See here what happens when you use __block with an object pointer variable. You can also learn more about __block 's __block Blocking Programming Themes .
Edit: Fixed bug with using __block using object pointers. Thanks @KevinDiTraglia for pointing this out.