虽然我在离开活动时释放资源,但是泄漏了上下文

时间:2016-09-06 16:44:58

标签: android memory-leaks leakcanary

我在Fragment内有一个Activity我正在使用一个名为ExoMedia的库,您可以在github上查看它here,它是谷歌的{{{}}包装器1}}。

在这个片段中,我声明了两个字段:

ExoPlayer

以后我实例化它们:

private EMVideoView emVideoView;
private VideoControlsMobile controlsMobile;

private void setupVideoView() { controlsMobile = new VideoControlsMobile(getActivity()); controlsMobile.setOnFullScreenListener(this); emVideoView.setVideoURI(Uri.parse(videoUrl)); emVideoView.setControls(controlsMobile); emVideoView.setOnCompletionListener(new OnCompletionListener() { @Override public void onCompletion() { isCompleted = true; emVideoView.setVideoURI(Uri.parse(videoUrl)); if (hasPolls || ((BaseActivity) getActivity()).hasPolls) { if ((((BaseActivity) getActivity()).isUserSignedIn())) { displayVotingDialog(); } } } }); } 上我试图让它们都为空,这样在泄漏活动时它们不会泄漏任何onDestroy

Context

但仍然在退出@Override public void onDestroy() { super.onDestroy(); if (controlsMobile != null) { controlsMobile.setOnFullScreenListener(null); controlsMobile = null; } if (emVideoView != null) { emVideoView.setControls(null); emVideoView.setOnPreparedListener(null); } } 时,我收到了Activity发布的LeakCanary泄漏报告:

Context

所以我有两个问题:

a)我在* com.oneproject.main.projects.ProjectsActivity has leaked: * GC ROOT android.os.MessageQueue.mMessages * references android.os.Message.next * references android.os.Message.next * references android.os.Message.callback * references com.devbrackets.android.exomedia.util.Repeater$PollRunnable.this$0 * references com.devbrackets.android.exomedia.util.Repeater.listener * references com.devbrackets.android.exomedia.ui.widget.VideoControls$6.this$0 (anonymous implementation of com.devbrackets.android.exomedia.util.Repeater$RepeatListener) * references com.devbrackets.android.exomedia.ui.widget.VideoControlsMobile.activity * leaks com.oneproject.main.projects.ProjectsActivity instance 中所做的不正确:将VideoControls对象设置为null,这样它就不会保持活着并且会泄漏内存。

b)这是一个错误还是我忘记发布一些错误并导致我这个错误?

由于

1 个答案:

答案 0 :(得分:1)

我认为该库有内存泄漏... VideoControls正在创建一个匿名的RepeatListener,但永远不会将其清空https://github.com/brianwernick/ExoMedia/blob/11b28992d4f819902b63949fe2cf54720edb172f/library/src/main/java/com/devbrackets/android/exomedia/ui/widget/VideoControls.java#L621

该侦听器将保留在您的活动创建和销毁之后。理想情况下,该库可以让您清空该侦听器。

保留发生的地方https://github.com/brianwernick/ExoMedia/blob/5dc963c8f49aa9e6bef318118e97e859b22979a4/library/src/main/java/com/devbrackets/android/exomedia/util/Repeater.java#L96