Skip to content

Commit e6ca8cb

Browse files
committed
Content: Memory (Edited A) - Why should weak be used in a block
1 parent c1e5ac5 commit e6ca8cb

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,12 @@ KVO stands for *Key-Value Observing*. It allows a controller or class to *observ
202202

203203
Sometimes it is necessary it capture self in a block such as when defining a callback block. However, since blocks maintain strong references to any captured objects including self, this may lead to a strong reference cycle and memory leak.
204204

205+
Instead, capturing a weak reference to self is recommended in order to avoid this issue:
206+
207+
```
208+
SomeBlock* __weak weakSelf = self;
209+
```
210+
205211
#### What is memory management handled on iOS?
206212

207213
iOS uses something called ARC which stands for Automatic Reference Counting. When an object is said to have a strong reference to it, ARC increase its retain count by 1. When the retain count of an object reaches 0, the object will typically be deallocated if there are no more strong references to it. Unlike garbage collection, ARC does not handle reference cycles automatically.

0 commit comments

Comments
 (0)