From 00257e969e009590a0ef11fca7bbc3e4e6269aa0 Mon Sep 17 00:00:00 2001 From: Stypox Date: Fri, 14 Jul 2023 18:34:20 +0200 Subject: [PATCH] Fix PlayerService leakead by Binder instance Also see https://stackoverflow.com/q/63787707 --- .../org/schabi/newpipe/player/PlayerService.java | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/player/PlayerService.java b/app/src/main/java/org/schabi/newpipe/player/PlayerService.java index d81301205..ad6c9405d 100644 --- a/app/src/main/java/org/schabi/newpipe/player/PlayerService.java +++ b/app/src/main/java/org/schabi/newpipe/player/PlayerService.java @@ -31,6 +31,8 @@ import android.util.Log; import org.schabi.newpipe.player.mediasession.MediaSessionPlayerUi; import org.schabi.newpipe.util.ThemeHelper; +import java.lang.ref.WeakReference; + /** * One service for all players. @@ -41,7 +43,7 @@ public final class PlayerService extends Service { private Player player; - private final IBinder mBinder = new PlayerService.LocalBinder(); + private final IBinder mBinder = new PlayerService.LocalBinder(this); /*////////////////////////////////////////////////////////////////////////// @@ -134,14 +136,19 @@ public final class PlayerService extends Service { return mBinder; } - public class LocalBinder extends Binder { + public static class LocalBinder extends Binder { + private final WeakReference playerService; + + LocalBinder(final PlayerService playerService) { + this.playerService = new WeakReference<>(playerService); + } public PlayerService getService() { - return PlayerService.this; + return playerService.get(); } public Player getPlayer() { - return PlayerService.this.player; + return playerService.get().player; } } }