From 6bed14863b53b2a7a6c20be69d571b9757eb5604 Mon Sep 17 00:00:00 2001 From: meiron03 Date: Mon, 12 Feb 2024 18:12:56 -0500 Subject: [PATCH] Add canaryleak to detect memory leaks unregister local broadcast receiver in GsrReservationsFragment when fragment is destroyed to prevent memory leak. TODO: remove LocalBroadcastManager entirely because that shit deprecated. remove LocalBroadCastManager from HomeFragment since it is unused (and was causing memory leaks) --- PennMobile/build.gradle | 2 ++ .../labs/pennmobile/GsrReservationsFragment.kt | 5 ++++- .../pennapps/labs/pennmobile/HomeFragment.kt | 17 ----------------- .../pennapps/labs/pennmobile/MainActivity.kt | 1 + 4 files changed, 7 insertions(+), 18 deletions(-) diff --git a/PennMobile/build.gradle b/PennMobile/build.gradle index 13987158..6e2a23a7 100644 --- a/PennMobile/build.gradle +++ b/PennMobile/build.gradle @@ -39,6 +39,8 @@ android { } dependencies { + debugImplementation 'com.squareup.leakcanary:leakcanary-android:3.0-alpha-1' + implementation fileTree(include: ['*.jar'], dir: 'libs') implementation(platform("com.google.firebase:firebase-bom:31.5.0")) diff --git a/PennMobile/src/main/java/com/pennapps/labs/pennmobile/GsrReservationsFragment.kt b/PennMobile/src/main/java/com/pennapps/labs/pennmobile/GsrReservationsFragment.kt index b7572960..82f83f45 100644 --- a/PennMobile/src/main/java/com/pennapps/labs/pennmobile/GsrReservationsFragment.kt +++ b/PennMobile/src/main/java/com/pennapps/labs/pennmobile/GsrReservationsFragment.kt @@ -130,5 +130,8 @@ class GsrReservationsFragment : Fragment() { getReservations() } } - + override fun onDestroy() { + super.onDestroy() + LocalBroadcastManager.getInstance(mActivity).unregisterReceiver(broadcastReceiver); + } } \ No newline at end of file diff --git a/PennMobile/src/main/java/com/pennapps/labs/pennmobile/HomeFragment.kt b/PennMobile/src/main/java/com/pennapps/labs/pennmobile/HomeFragment.kt index 7e342b83..3c2f375d 100644 --- a/PennMobile/src/main/java/com/pennapps/labs/pennmobile/HomeFragment.kt +++ b/PennMobile/src/main/java/com/pennapps/labs/pennmobile/HomeFragment.kt @@ -1,13 +1,8 @@ package com.pennapps.labs.pennmobile import android.content.* -import android.content.BroadcastReceiver -import android.content.Context -import android.content.Intent -import android.content.IntentFilter import android.os.Bundle import android.os.Handler -import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup @@ -17,7 +12,6 @@ import androidx.fragment.app.viewModels import androidx.lifecycle.Lifecycle import androidx.lifecycle.lifecycleScope import androidx.lifecycle.repeatOnLifecycle -import androidx.localbroadcastmanager.content.LocalBroadcastManager import androidx.preference.PreferenceManager import androidx.recyclerview.widget.LinearLayoutManager import com.pennapps.labs.pennmobile.adapters.HomeAdapter @@ -47,10 +41,6 @@ class HomeFragment : Fragment() { mActivity = activity as MainActivity sharedPreferences = PreferenceManager.getDefaultSharedPreferences(mActivity) - - LocalBroadcastManager - .getInstance(mActivity) - .registerReceiver(broadcastReceiver, IntentFilter("refresh")) } override fun onCreateView( @@ -188,13 +178,6 @@ class HomeFragment : Fragment() { } } - - private val broadcastReceiver = object : BroadcastReceiver() { - override fun onReceive(context: Context?, intent: Intent?) { - getHomePage() - } - } - override fun onResume() { super.onResume() mActivity.removeTabs() diff --git a/PennMobile/src/main/java/com/pennapps/labs/pennmobile/MainActivity.kt b/PennMobile/src/main/java/com/pennapps/labs/pennmobile/MainActivity.kt index e6a976d5..8f69c3ba 100644 --- a/PennMobile/src/main/java/com/pennapps/labs/pennmobile/MainActivity.kt +++ b/PennMobile/src/main/java/com/pennapps/labs/pennmobile/MainActivity.kt @@ -374,6 +374,7 @@ fun isOnline(context: Context?): Boolean { return false } + /** Shows an error sneaker given a view group with an optional retry function */ fun ViewGroup.showSneakerToast(message: String, doOnRetry: (() -> Unit)?, sneakerColor: Int) { val sneaker = Sneaker.with(this)