Merge pull request #10232 from Stypox/leakcanary-fix

Fix LeakCanary startup in debug builds and fix a memory leak
This commit is contained in:
Tobi 2023-07-17 19:42:24 +02:00 committed by GitHub
commit 2f0ed7f3b7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 13 additions and 18 deletions

View file

@ -288,8 +288,8 @@ dependencies {
/** Debugging **/ /** Debugging **/
// Memory leak detection // Memory leak detection
implementation "com.squareup.leakcanary:leakcanary-object-watcher-android:${leakCanaryVersion}" debugImplementation "com.squareup.leakcanary:leakcanary-object-watcher-android:${leakCanaryVersion}"
implementation "com.squareup.leakcanary:plumber-android:${leakCanaryVersion}" debugImplementation "com.squareup.leakcanary:plumber-android:${leakCanaryVersion}"
debugImplementation "com.squareup.leakcanary:leakcanary-android-core:${leakCanaryVersion}" debugImplementation "com.squareup.leakcanary:leakcanary-android-core:${leakCanaryVersion}"
// Debug bridge for Android // Debug bridge for Android
debugImplementation "com.facebook.stetho:stetho:${stethoVersion}" debugImplementation "com.facebook.stetho:stetho:${stethoVersion}"

View file

@ -3,7 +3,6 @@ package org.schabi.newpipe
import androidx.preference.PreferenceManager import androidx.preference.PreferenceManager
import com.facebook.stetho.Stetho import com.facebook.stetho.Stetho
import com.facebook.stetho.okhttp3.StethoInterceptor import com.facebook.stetho.okhttp3.StethoInterceptor
import leakcanary.AppWatcher
import leakcanary.LeakCanary import leakcanary.LeakCanary
import okhttp3.OkHttpClient import okhttp3.OkHttpClient
import org.schabi.newpipe.extractor.downloader.Downloader import org.schabi.newpipe.extractor.downloader.Downloader
@ -13,8 +12,6 @@ class DebugApp : App() {
super.onCreate() super.onCreate()
initStetho() initStetho()
// Give each object 10 seconds to be GC'ed, before LeakCanary gets nosy on it
AppWatcher.manualInstall(this, retainedDelayMillis = 10000)
LeakCanary.config = LeakCanary.config.copy( LeakCanary.config = LeakCanary.config.copy(
dumpHeap = PreferenceManager dumpHeap = PreferenceManager
.getDefaultSharedPreferences(this).getBoolean( .getDefaultSharedPreferences(this).getBoolean(

View file

@ -12,7 +12,6 @@ import androidx.fragment.app.FragmentManager;
import icepick.Icepick; import icepick.Icepick;
import icepick.State; import icepick.State;
import leakcanary.AppWatcher;
public abstract class BaseFragment extends Fragment { public abstract class BaseFragment extends Fragment {
protected final String TAG = getClass().getSimpleName() + "@" + Integer.toHexString(hashCode()); protected final String TAG = getClass().getSimpleName() + "@" + Integer.toHexString(hashCode());
@ -77,14 +76,6 @@ public abstract class BaseFragment extends Fragment {
protected void onRestoreInstanceState(@NonNull final Bundle savedInstanceState) { protected void onRestoreInstanceState(@NonNull final Bundle savedInstanceState) {
} }
@Override
public void onDestroy() {
super.onDestroy();
AppWatcher.INSTANCE.getObjectWatcher().expectWeaklyReachable(
this, "Watch for leaks from destroyed fragments.");
}
/*////////////////////////////////////////////////////////////////////////// /*//////////////////////////////////////////////////////////////////////////
// Init // Init
//////////////////////////////////////////////////////////////////////////*/ //////////////////////////////////////////////////////////////////////////*/

View file

@ -31,6 +31,8 @@ import android.util.Log;
import org.schabi.newpipe.player.mediasession.MediaSessionPlayerUi; import org.schabi.newpipe.player.mediasession.MediaSessionPlayerUi;
import org.schabi.newpipe.util.ThemeHelper; import org.schabi.newpipe.util.ThemeHelper;
import java.lang.ref.WeakReference;
/** /**
* One service for all players. * One service for all players.
@ -41,7 +43,7 @@ public final class PlayerService extends Service {
private Player player; 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; return mBinder;
} }
public class LocalBinder extends Binder { public static class LocalBinder extends Binder {
private final WeakReference<PlayerService> playerService;
LocalBinder(final PlayerService playerService) {
this.playerService = new WeakReference<>(playerService);
}
public PlayerService getService() { public PlayerService getService() {
return PlayerService.this; return playerService.get();
} }
public Player getPlayer() { public Player getPlayer() {
return PlayerService.this.player; return playerService.get().player;
} }
} }
} }