Android DatePickerDialog中存在内存泄漏。如何确定问题所在?
我正在我的应用程序中使用。我无法找到泄漏的根本原因<根据日志显示,code>DatePickerDialog正在泄漏。我不确定lib是否有问题,但我认为我做错了什么 我有一个显示当前日期的Android DatePickerDialog中存在内存泄漏。如何确定问题所在?,android,leakcanary,Android,Leakcanary,我正在我的应用程序中使用。我无法找到泄漏的根本原因DatePickerDialog正在泄漏。我不确定lib是否有问题,但我认为我做错了什么 我有一个显示当前日期的视图。用户可以单击它,然后显示DatePickerDialog。然后,他/她选择一天并单击日期选择器对话框的确定按钮。我收到日期,格式化,然后提交。几秒钟后,泄漏金丝雀lib说嘿,你泄漏了。如果你能告诉我我的代码出了什么问题,那就太好了 这是我的自定义小部件 import android.content.Context import a
视图
。用户可以单击它,然后显示DatePickerDialog
。然后,他/她选择一天并单击日期选择器对话框的确定按钮。我收到日期,格式化,然后提交。几秒钟后,泄漏金丝雀lib说嘿,你泄漏了。如果你能告诉我我的代码出了什么问题,那就太好了
这是我的自定义小部件
import android.content.Context
import androidx.core.content.ContextCompat
import androidx.fragment.app.FragmentManager
import com.atco.forsite.R
import com.atco.forsite.screens.utility.DatePickerOFIState.NOW_FUTURE
import com.atco.forsite.screens.utility.DatePickerOFIState.PAST_NOW
import com.jakewharton.rxrelay2.PublishRelay
import com.wdullaer.materialdatetimepicker.date.DatePickerDialog
import java.util.Calendar
enum class DatePickerOFIState {
PAST_NOW,
NOW_FUTURE
}
class DatePickerOFI(
private val context: Context,
private val fm: FragmentManager,
private val state: DatePickerOFIState
) : DatePickerDialog.OnDateSetListener {
/**
* Subscribe to this observer in order to be notified when the result is ready.
*/
val relay: PublishRelay<Calendar> = PublishRelay.create()
private val datePicker: DatePickerDialog
companion object {
const val TAG = "DatePickerDialog"
}
init {
val now = Calendar.getInstance()
datePicker = DatePickerDialog.newInstance(
this,
now.get(Calendar.YEAR),
now.get(Calendar.MONTH),
now.get(Calendar.DAY_OF_MONTH)
)
datePicker.version = DatePickerDialog.Version.VERSION_2
datePicker.accentColor = ContextCompat.getColor(context, R.color.primaryBlue)
datePicker.setOkColor(ContextCompat.getColor(context, R.color.white))
datePicker.setCancelColor(ContextCompat.getColor(context, R.color.white))
when (state) {
PAST_NOW -> datePicker.maxDate = now
NOW_FUTURE -> datePicker.minDate = now
}
}
fun show() {
datePicker.show(fm, TAG)
}
override fun onDateSet(view: DatePickerDialog?, year: Int, monthOfYear: Int, dayOfMonth: Int) {
val calendar = Calendar.getInstance()
calendar.set(year, monthOfYear, dayOfMonth)
relay.accept(calendar)
}
}
泄漏痕迹:
┬───
│ GC Root: System class
│
├─ android.view.inputmethod.InputMethodManager class
│ Leaking: NO (InputMethodManager↓ is not leaking and a class is never leaking)
│ ↓ static InputMethodManager.sInstance
├─ android.view.inputmethod.InputMethodManager instance
│ Leaking: NO (DecorView↓ is not leaking and InputMethodManager is a singleton)
│ ↓ InputMethodManager.mCurRootView
├─ com.android.internal.policy.DecorView instance
│ Leaking: NO (LinearLayout↓ is not leaking and View attached)
│ mContext instance of com.android.internal.policy.DecorContext, wrapping activity com.atco.forsite.screens.records.safetyExchange.SafetyExchangeActivity with mDestroyed = false
│ Parent android.view.ViewRootImpl not a android.view.View
│ View#mParent is set
│ View#mAttachInfo is not null (view attached)
│ View.mWindowAttachCount = 1
│ ↓ DecorView.mContentRoot
├─ android.widget.LinearLayout instance
│ Leaking: NO (SafetyExchangeActivity↓ is not leaking and View attached)
│ mContext instance of com.atco.forsite.screens.records.safetyExchange.SafetyExchangeActivity with mDestroyed = false
│ View.parent com.android.internal.policy.DecorView attached as well
│ View#mParent is set
│ View#mAttachInfo is not null (view attached)
│ View.mWindowAttachCount = 1
│ ↓ LinearLayout.mContext
├─ com.atco.forsite.screens.records.safetyExchange.SafetyExchangeActivity instance
│ Leaking: NO (Activity#mDestroyed is false)
│ ↓ SafetyExchangeActivity.datePicker
│ ~~~~~~~~~~
├─ com.atco.forsite.screens.utility.DatePickerOFI instance
│ Leaking: UNKNOWN
│ ↓ DatePickerOFI.datePicker
│ ~~~~~~~~~~
╰→ com.wdullaer.materialdatetimepicker.date.DatePickerDialog instance
Leaking: YES (ObjectWatcher was watching this because com.wdullaer.materialdatetimepicker.date.DatePickerDialog received Fragment#onDestroy() callback and Fragment#mFragmentManager is null)
key = dd153a49-8e66-45f7-8736-16342134e7f6
watchDurationMillis = 5297
retainedDurationMillis = 297
METADATA
Build.VERSION.SDK_INT: 29
Build.MANUFACTURER: Google
LeakCanary version: 2.2
App process name: com.atco.forsite
Analysis duration: 8193 ms
根据泄漏痕迹,我认为问题的根源是这条线路
private val datePicker: DatePickerDialog
在DatePickerOFI
类中
作为一种解决方案,我认为一旦您的工作完成,即对话框被取消,您需要将datePicker
实例变量置零 它可能应该是var
:
private lateinit var datePicker: DatePickerDialog
并且它应该被解构为onDestroy
:
override fun onDestroy() {
datePicker = null;
super.onDestroy()
}
由于这里缺少一些代码,所以我猜测,即使对话框完成,您可能仍然订阅了relay。您是对的。我更改了DatePickerOFI的实例创建,以便在作业完成时使其无效,如果用户决定第二次更改,Show()方法将再次创建其实例。谢谢你,谢谢马丁。我相信问题出在我的DatePickerOFI课上。我参加活动时收到了Leack通知。因此,在“活动”中取消它没有帮助。@Hesam它可能仍然报告潜在泄漏,但在“活动”被销毁时不会泄漏。想一想在这两种情况下都可以
null
it,一个选项是最快的,另一个是最新的。
override fun onDestroy() {
datePicker = null;
super.onDestroy()
}