RxSwiftCommunity / RxKeyboard

Reactive Keyboard in iOS
MIT License
1.6k stars 175 forks source link

Fix memory leak error in the examples of the README file #87

Closed alspirichev closed 5 years ago

alspirichev commented 5 years ago

I think we should use a weak reference (not a strong) in our examples. By default, and when we use square brackets without explicitly specifying the type of reference, Swift uses a strong reference to external objects.

freak4pc commented 5 years ago

Why is the strong reference an issue? There's no retain cycle here.

The closure captures scrollView, but scrollView doesn't capture the closure (e.g. no cycle in dependencies, no problem for each of them to be individually released).

When using self, specifically, the closure captures self and self captures the closure - which is where the cycle happens.

Did you spot any memory issues with using the existing capture groups?

Thanks for your feedback!

M0rtyMerr commented 5 years ago

Fully agree with @freak4pc

alspirichev commented 5 years ago

Ok, but why then do we explicitly use the capture list? without explicitly specifying the type of reference in it. We can omit the capture list in these examples.

M0rtyMerr commented 5 years ago

@alspirichev please read more docs about capture list. Reference in capture list is a copy of original reference. So, that's not a reference on self. That prevents reference cycle and memory leak. Feel free to ask if you have some more questions :)

freak4pc commented 5 years ago

You're in a closure. If you won't capture scrollView, you'll have to reference self.scrollView explicitly - which would create a retain cycle between self and the closure.