Eureka icon indicating copy to clipboard operation
Eureka copied to clipboard

Every BaseRow subclass leaks by default

Open MutatingFunc opened this issue 5 years ago • 2 comments

Eureka version: 5.1.0

Tool used: Xcode memory graph

Steps to reproduce:

  • Create a basic form with Eureka, with row models inheriting from BaseRow.
  • Have this be pushed from a root view, so it is possible to destroy & recreate the form.
  • Don't access leadingSwipe/trailingSwipe within the form.
  • Access the form several times.
  • Row instances are leaked.

Location of cause: Line 104 of BaseRow.swift public lazy var trailingSwipe = SwipeConfiguration(self)

Line 107 of BaseRow.swift private lazy var _leadingSwipe = SwipeConfiguration(self)

Reason: Lazy variables implicitly create a closure to produce the initial value. Because the initial value includes a reference to self, the closure captures a strong reference to self, producing a circular reference.

~Solution is unclear, generally f(self) can be replaced by {[weak self] in guard let self = self else {return}; f(self)}, but in this case there's a return value which cannot be Optional and SwipeConfiguration cannot be mocked, so some rearchitecting is required.~

You could in fact just remove the lazy keyword if that isn't required, or use {[unowned self] in SwipeConfiguration(self)}().

MutatingFunc avatar Oct 17 '19 15:10 MutatingFunc

It seems like something in Eureka must access the values, because this isn't actually happening 100% of the time, just in a lot of cases.

MutatingFunc avatar Oct 17 '19 15:10 MutatingFunc

Opened a PR: https://github.com/xmartlabs/Eureka/pull/1930

MutatingFunc avatar Oct 18 '19 15:10 MutatingFunc