What is the difference between a __weak and a __block reference?
Asked Answered
A

4

82

I'm reading Xcode's documentation, and here is something that puzzles me:

__block typeof(self) tmpSelf = self;
[self methodThatTakesABlock:^ {
    [tmpSelf doSomething];
}];

The following is copied from the documentation:

A block forms a strong reference to variables it captures. If you use self within a block, the block forms a strong reference to self, so if self also has a strong reference to the block (which it typically does), a strong reference cycle results. To avoid the cycle, you need to create a weak (or __block) reference to self outside the block, as in the example above.

I don't understand what does 'a weak (or __block)' mean?

Is

__block typeof(self) tmpSelf = self;

and

__weak typeof(self) tmpSelf = self;

exactly the same here?

I found another piece in the document:

Note: In a garbage-collected environment, if you apply both __weak and __block modifiers to a variable, then the block will not ensure that it is kept alive.

So, I'm totally puzzled.

Azotobacter answered 2/8, 2012 at 8:2 Comment(0)
F
110

From the docs about __block

__block variables live in storage that is shared between the lexical scope of the variable and all blocks and block copies declared or created within the variable’s lexical scope. Thus, the storage will survive the destruction of the stack frame if any copies of the blocks declared within the frame survive beyond the end of the frame (for example, by being enqueued somewhere for later execution). Multiple blocks in a given lexical scope can simultaneously use a shared variable.

From the docs about __weak

__weak specifies a reference that does not keep the referenced object alive. A weak reference is set to nil when there are no strong references to the object.

So they are technically different things. __block is to stop your variable being copied from your external scope into your block scope. __weak is a self delimiting weak pointer.

Note I said technically, because for your case they will do (almost) the same thing. The only difference is if you are using ARC or not. If your project uses ARC and is only for iOS4.3 and above, use __weak. It ensures the reference is set to nil if the global scope reference is releases somehow. If your project doesn't use ARC or is for older OS versions, use __block.

There is a subtle difference here, make sure you understand it.

EDIT: Another piece to the puzzle is __unsafe_unretained. This modifier is almost the same as __weak but for pre 4.3 runtime environments. HOWEVER, it is not set to nil and can leave you with hanging pointers.

Frenchy answered 2/8, 2012 at 8:19 Comment(3)
Is this still applicable to iOS7 using ARC? I ran a profiler and I see that my controllers are being released even if I don't use __block or __weak and reference self in a block.Outmost
For someone who wants to see doc: developer.apple.com/library/ios/releasenotes/ObjectiveC/… , developer.apple.com/library/ios/documentation/Cocoa/Conceptual/…Lammastide
How about using them together? __block _weak NSString *strEg;?Asclepius
G
6

In manual reference counting mode, __block id x; has the effect of not retaining x. In ARC mode, __block id x; defaults to retaining x (just like all other values). To get the manual reference counting mode behavior under ARC, you could use __unsafe_unretained __block id x;. As the name __unsafe_unretained implies, however, having a non-retained variable is dangerous (because it can dangle) and is therefore discouraged. Two better options are to either use __weak (if you don’t need to support iOS 4 or OS X v10.6), or set the __block value to nil to break the retain cycle.

apple docs

Gerda answered 31/7, 2013 at 8:32 Comment(0)
M
1

When using self in block, should use __weak, not __block as it may retain self.

In case you need strong self, then you can use like this:

__weak typeof(self) *weakSelf = self;
[self methodThatTakesABlock:^{
    if (weakSelf) {
        __strong typeof(self) *strongSelf = weakSelf;
        [strongSelf doSomething];
    }
}];
Murillo answered 1/5, 2019 at 22:44 Comment(0)
B
0

Beside other answers on __block vs __weak, there is another way to avoid retain cycle in your scenario.

@weakify(self);
[self methodThatTakesABlock:^ {
    @strongify(self);
    [self doSomething];
}];

More Info about @Weakify @Strongify Macro

Bipropellant answered 26/1, 2018 at 8:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.