Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix LeakCanary startup in debug builds and fix a memory leak #10232

Merged
merged 4 commits into from
Jul 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}
}
Loading