I have some guidelines I apply to myself to help reduce cyclic references caused by closures capturing self.
- I never explicitly refer to
self(as inself.somePropertyorself.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 becauseself.in a closure means the closure capturesselfand holds a strong reference toself. 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:
- in
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
self.
This means I can run grep -R "self\." . and do a quick scan when I suspect a memory leak.
.
.