Conductor: Kotlin - lateinit memory leak issues
Conductor should boldly advertise (I don’t think it’s made obvious enough for new users) that its instances survive configuration changes (stark contrast to Activity/Fragment/View) - this has huge implications for dagger + kotlin users.
class SalesController : BaseController, SalesView {
@Inject lateinit var viewBinder: SalesController.ViewBinder
@Inject lateinit var renderer: SalesRenderer
@Inject lateinit var presenter: SalesPresenter
lateinit private var component: SalesScreenComponent
override var state = SalesScreen.State.INITIAL //only property that I want to survive config changes
fun onCreateView(): View { /** lateinit variables are set here */ }
fun onDestroyView() { /** lateinit variables need to be dereferenced here, or we have a memory leak */ }
}
My lateinit properties are injected by dagger, and I need to set them to null in onDestroyView - or have a memory leak. This however is not possible in kotlin, as far as I am aware (without reflection). I could make these properties nullable, but that would defeat the purpose of Kotlin’s null safety.
I’m not quite sure how to solve this. Ideally there could be some type of annotation processor that would null out specific variables automatically in onDestroyView
About this issue
- Original URL
- State: open
- Created 7 years ago
- Reactions: 5
- Comments: 27 (11 by maintainers)
Ultimately, this is the solution I’ve implemented, and I’m happy to report it works quite well. If I want a property to survive a configuration change, I just don’t add the
by Ref(ref)delegate.One could also write a
ResetableReferencewithout a “manager”. Just add a Controller LifecycleListener internally in ResetableReference.Just throwing another thank you on there @ZakTaccardi. I had another solution going that I was stubborn about swapping out until today. Your resettable reference made things so much nicer!
https://youtrack.jetbrains.com/issue/KT-19621