Mes
Mes

Reputation: 1691

Leaking context although I'm releasing resources when leaving activity

I have a Fragment inside an Activity where I'm using a library named ExoMedia you can check it here on github and its a wrapper for google's ExoPlayer.

In this Fragment I declare two fields :

private EMVideoView emVideoView;
private VideoControlsMobile controlsMobile;

and later I instantiate them :

    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();
                }
            }
        }
    });

}

where on onDestroy I'm trying to make them both null so they won't leak any Context when leaking the Activity :

@Override
public void onDestroy() {
    super.onDestroy();
    if (controlsMobile != null) {
        controlsMobile.setOnFullScreenListener(null);
        controlsMobile = null;
    }
    if (emVideoView != null) {
        emVideoView.setControls(null);
        emVideoView.setOnPreparedListener(null);
    }
}

but still when exiting my Activity I got a report from LeakCanary for leaking Context :

* 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

so I 've got two questions :

a) Isn't what I'm doing in onDestroy() correct : setting VideoControls object to null so it won't kept alive and leak memory.

b) Is this a bug or I forgot to release something and its causing me this error ?

Thanks

Upvotes: 0

Views: 505

Answers (1)

FriendlyMikhail
FriendlyMikhail

Reputation: 2927

I think the library has a memory leak... VideoControls is creating an anonymous RepeatListener but never nulling it out https://github.com/brianwernick/ExoMedia/blob/11b28992d4f819902b63949fe2cf54720edb172f/library/src/main/java/com/devbrackets/android/exomedia/ui/widget/VideoControls.java#L621.

That listener is getting retained past your activity creation and destruction. Ideally the library would let you null out that listener.

Here's where the retention happens https://github.com/brianwernick/ExoMedia/blob/5dc963c8f49aa9e6bef318118e97e859b22979a4/library/src/main/java/com/devbrackets/android/exomedia/util/Repeater.java#L96

Upvotes: 1

Related Questions