Skip to content

Commit

Permalink
Merge pull request #10232 from Stypox/leakcanary-fix
Browse files Browse the repository at this point in the history
Fix LeakCanary startup in debug builds and fix a memory leak
  • Loading branch information
TobiGr authored Jul 17, 2023
2 parents d80e531 + 5d6158e commit 2f0ed7f
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 18 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,8 @@ dependencies {

/** Debugging **/
// Memory leak detection
implementation "com.squareup.leakcanary:leakcanary-object-watcher-android:${leakCanaryVersion}"
implementation "com.squareup.leakcanary:plumber-android:${leakCanaryVersion}"
debugImplementation "com.squareup.leakcanary:leakcanary-object-watcher-android:${leakCanaryVersion}"
debugImplementation "com.squareup.leakcanary:plumber-android:${leakCanaryVersion}"
debugImplementation "com.squareup.leakcanary:leakcanary-android-core:${leakCanaryVersion}"
// Debug bridge for Android
debugImplementation "com.facebook.stetho:stetho:${stethoVersion}"
Expand Down
3 changes: 0 additions & 3 deletions app/src/debug/java/org/schabi/newpipe/DebugApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package org.schabi.newpipe
import androidx.preference.PreferenceManager
import com.facebook.stetho.Stetho
import com.facebook.stetho.okhttp3.StethoInterceptor
import leakcanary.AppWatcher
import leakcanary.LeakCanary
import okhttp3.OkHttpClient
import org.schabi.newpipe.extractor.downloader.Downloader
Expand All @@ -13,8 +12,6 @@ class DebugApp : App() {
super.onCreate()
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(
dumpHeap = PreferenceManager
.getDefaultSharedPreferences(this).getBoolean(
Expand Down
9 changes: 0 additions & 9 deletions app/src/main/java/org/schabi/newpipe/BaseFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

import icepick.Icepick;
import icepick.State;
import leakcanary.AppWatcher;

public abstract class BaseFragment extends Fragment {
protected final String TAG = getClass().getSimpleName() + "@" + Integer.toHexString(hashCode());
Expand Down Expand Up @@ -77,14 +76,6 @@ public void onSaveInstanceState(@NonNull final Bundle outState) {
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
//////////////////////////////////////////////////////////////////////////*/
Expand Down
15 changes: 11 additions & 4 deletions app/src/main/java/org/schabi/newpipe/player/PlayerService.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import org.schabi.newpipe.player.mediasession.MediaSessionPlayerUi;
import org.schabi.newpipe.util.ThemeHelper;

import java.lang.ref.WeakReference;


/**
* One service for all players.
Expand All @@ -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);


/*//////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -134,14 +136,19 @@ public IBinder onBind(final Intent intent) {
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() {
return PlayerService.this;
return playerService.get();
}

public Player getPlayer() {
return PlayerService.this.player;
return playerService.get().player;
}
}
}

0 comments on commit 2f0ed7f

Please sign in to comment.