I have some guidelines I apply to myself to help reduce cyclic references caused by closures capturing
- I never explicitly refer to
self.someFunc()) unless I absolutely have to.
- I have a simple rule of thumb, if I see a
self.in a closure, I double check to make sure it is necessary. This is because
self.in a closure means the closure captures
selfand holds a strong reference to
self. Most of the time, this can lead to cyclic references either now or later. I try my best to avoid them.
Because of (1) above,
self. will only appear:
init()and its variations
- when I need to assign to a new local variable in a function and the new local variable has the same name as a function or a property
- when closure captures
This means I can run
grep -R "self\." . and do a quick scan when I suspect a memory leak.