Fixed
Status Update
Comments
il...@google.com <il...@google.com> #2
You should not be doing anything beyond onCleared()
being called - that would already be a sign that you are leaking work and something you'll want to fix regardless - your background task (thread, coroutine, rx-java chain) should already be cancelled as part of your onCleared
, either by manually cancelling it or my adding a Closeable
to do that for you, just like how viewModelScope
automatically cancels coroutines launched in that scope as part of onCleared()
.
That being said, we can still take a look at this.
sa...@google.com <sa...@google.com>
ap...@google.com <ap...@google.com> #3
Project: platform/frameworks/support
Branch: androidx-main
commit 6f60c005dacf851456a18a639ab34b00b3e268a0
Author: Sanura Njaka <sanura@google.com>
Date: Wed Aug 23 11:29:03 2023
Ensure added Closeables closed even if viewmodel cleared
Although developers should not be adding any logic after
viewmodel.onCleared() has been called, we should ensure
that Closeables are still closed if they are attempted
to be added to the ViewModel to prevent potential leaks.
RelNote: "`ViewModel`'s `addCloseable()` now checks whether
the view model has already been cleared before adding the
closeable to prevent leaks."
Test: testAddCloseableAlreadyClearedVM
Fixes: 280294730
Change-Id: I4712ee3600acbf04506af46e6ac479a617cebd86
M lifecycle/lifecycle-viewmodel/src/main/java/androidx/lifecycle/ViewModel.java
M lifecycle/lifecycle-viewmodel/src/test/java/androidx/lifecycle/ViewModelTest.kt
https://android-review.googlesource.com/2724173
Branch: androidx-main
commit 6f60c005dacf851456a18a639ab34b00b3e268a0
Author: Sanura Njaka <sanura@google.com>
Date: Wed Aug 23 11:29:03 2023
Ensure added Closeables closed even if viewmodel cleared
Although developers should not be adding any logic after
viewmodel.onCleared() has been called, we should ensure
that Closeables are still closed if they are attempted
to be added to the ViewModel to prevent potential leaks.
RelNote: "`ViewModel`'s `addCloseable()` now checks whether
the view model has already been cleared before adding the
closeable to prevent leaks."
Test: testAddCloseableAlreadyClearedVM
Fixes: 280294730
Change-Id: I4712ee3600acbf04506af46e6ac479a617cebd86
M lifecycle/lifecycle-viewmodel/src/main/java/androidx/lifecycle/ViewModel.java
M lifecycle/lifecycle-viewmodel/src/test/java/androidx/lifecycle/ViewModelTest.kt
il...@google.com <il...@google.com> #4
This has been fixed internally and will be available in Lifecycle 2.7.0-alpha02.
pr...@google.com <pr...@google.com> #5
The following release(s) address this bug.It is possible this bug has only been partially addressed:
androidx.lifecycle:lifecycle-viewmodel:2.7.0-alpha02
ph...@gmail.com <ph...@gmail.com> #6
เห็นไม่ได้
Description
Component used: androidx.lifecycle:lifecycle-viewmodel
Version used: 2.6.1
Devices/Android versions reproduced on: n/a
Current implementation of
addCloseable
doesn't have a check ofmCleared
, unlike ofputTagIfAbsent
. It may lead to memory leaks, because user can not add check ofmCleared
and close Closeable by its own. For example, if we start background task (thread, coroutine, rx-java chain), it is possible for this task to be active afteronClear
invocation (even this time is small) and add Closeable object afteronClear
. In this case,Closeable.close()
will not be invoked.Could be this check added to
addCloseable
implementation?