本文介绍了InputMethodManager中的LeakCanary报告泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找出它在我的应用程序中的漏洞,但是我不确定这是从哪里来的.

I am trying to figure it out a leak in my app but I am not sure were this comes from.

LeakCanary告诉我可以忽略它.是吗?

LeakCanary is telling me that I can ignore it. Is that right?

01-06 12:04:56.580 6935-9159/com.mypackage D/LeakCanary: * LEAK CAN BE IGNORED.
01-06 12:04:56.580 6935-9159/com.mypackage D/LeakCanary: * com.mypackage.ui.map.MapComponentFragment has leaked:
01-06 12:04:56.580 6935-9159/com.mypackage D/LeakCanary: * GC ROOT android.view.inputmethod.InputMethodManager$1.this$0 (anonymous class extends com.android.internal.view.IInputMethodClient$Stub)
01-06 12:04:56.580 6935-9159/com.mypackage D/LeakCanary: * references android.view.inputmethod.InputMethodManager.mCurRootView
01-06 12:04:56.580 6935-9159/com.mypackage D/LeakCanary: * references com.android.internal.policy.impl.PhoneWindow$DecorView.mContext
01-06 12:04:56.580 6935-9159/com.mypackage D/LeakCanary: * references com.mypackage.ui.MainActivity.mFragments
01-06 12:04:56.580 6935-9159/com.mypackage D/LeakCanary: * references android.app.FragmentManagerImpl.mAdded
01-06 12:04:56.580 6935-9159/com.mypackage D/LeakCanary: * references java.util.ArrayList.array
01-06 12:04:56.580 6935-9159/com.mypackage D/LeakCanary: * references array java.lang.Object[].[0]
01-06 12:04:56.580 6935-9159/com.mypackage D/LeakCanary: * leaks com.mypackage.ui.map.MapComponentFragment instance
01-06 12:04:56.580 6935-9159/com.mypackage D/LeakCanary: * Reference Key: 0790f013-1c87-4d5f-8c10-db277187e3ce
01-06 12:04:56.580 6935-9159/com.mypackage D/LeakCanary: * Device: samsung samsung SM-N910C treltexx
01-06 12:04:56.580 6935-9159/com.mypackage D/LeakCanary: * Android Version: 5.1.1 API: 22 LeakCanary: 1.4-SNAPSHOT 2714152
01-06 12:04:56.580 6935-9159/com.mypackage D/LeakCanary: * Durations: watch=5085ms, gc=149ms, heap dump=2562ms, analysis=10313ms

此片段导致了一些泄漏,但我已修复它们.我不能让它消失.

There were several leaks caused by this fragment, but I fix them. This one I can't make it disappear.

有任何线索吗?

编辑

还有一件事,如果我想忽略它,这应该足够了吗?

One more thing, if I want to ignore it this should be enough?

.instanceField("android.view.inputmethod.InputMethodManager", "mCurRootView")

但仍显示在LeakCanary中

But is still showing up in LeakCanary

显然,此泄漏发生在AndroidExcludeRef中,但仍在报告中. https://github.com/square/leakcanary/issues/322

Apparently this leak is in AndroidExcludeRef but still being reported.https://github.com/square/leakcanary/issues/322

推荐答案

/**
 * call this method in activity onDestroy() method.
*/
public static void fixInputMethod(Context context) {
    if (context == null) {
        return;
    }
    InputMethodManager inputMethodManager = null;
    try {
        inputMethodManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
    } catch (Throwable th) {
        th.printStackTrace();
    }
    if (inputMethodManager == null) {
        return;
    }
    Field[] declaredFields = inputMethodManager.getClass().getDeclaredFields();
    for (Field declaredField : declaredFields) {
        try {
            if (!declaredField.isAccessible()) {
                declaredField.setAccessible(true);
            }
            Object obj = declaredField.get(inputMethodManager);
            if (obj == null || !(obj instanceof View)) {
                continue;
            }
            View view = (View) obj;
            if (view.getContext() == context) {
                declaredField.set(inputMethodManager, null);
            } else {
                continue;
            }
        } catch (Throwable th) {
            th.printStackTrace();
        }
    }
}

这篇关于InputMethodManager中的LeakCanary报告泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 08:52