Memory leak using "LifecycleExt.kt"
See original GitHub issueHello Arkadiy, thank you very much for such a great library!
It seems to me there is a memory leak in this file: https://github.com/arkivanov/MVIKotlin/blob/bad123550ff7e451708280efbc4394ffdc347107/mvikotlin/src/commonMain/kotlin/com/arkivanov/mvikotlin/core/lifecycle/LifecycleExt.kt#L49
Probably for all these methods: Lifecycle.doOnResumePause
, Lifecycle.doOnPause
, Lifecycle.doOnStartStop
, Lifecycle.doOnStop
.
As soon as an anonymous callback instance is created, there is no chance to remove this object until onDestroy
event.
And if someone uses these methods in onStart
then on each call a new callback will be added to LifecycleRegistry
.
As a variant to fix that, I can suggest returning Disposable
to let a client to remove/unsubscribe from LifecycleRegistry
in this way:
inline fun Lifecycle.doOnResumePause(crossinline onResume: () -> Unit, crossinline onPause: () -> Unit): Disposable {
val callbacks = object : DefaultLifecycleCallbacks {
override fun onResume() {
onResume.invoke()
}
override fun onPause() {
onPause.invoke()
}
}
subscribe(callbacks)
return CancellableDisposable { unsubscribe(callbacks) }
}
// Then from the client side we can call `disposable?.dispose()` to remove added callback.
At the same time there is no such issue with Lifecycle.doOnDestroy
and Lifecycle.doOnCreateDestroy
(at least on Android platform) because Android LifecycleRegistry
removes a callback automatically after onDestroy
event.
@arkivanov does it make sense?
Issue Analytics
- State:
- Created 2 years ago
- Comments:7 (5 by maintainers)
Top GitHub Comments
Will be delivered in version
2.1.0
Fixed via #259