From a0533c4babf867719d0953233df171b1e1a526c7 Mon Sep 17 00:00:00 2001 From: meiron03 Date: Tue, 13 Feb 2024 18:09:19 -0500 Subject: [PATCH] Fix memory leaks (#603) * 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) * Fix more memory leaks lol * remove unused import --- PennMobile/build.gradle | 2 ++ .../labs/pennmobile/GsrReservationsFragment.kt | 5 ++++- .../pennapps/labs/pennmobile/HomeFragment.kt | 17 ----------------- .../pennapps/labs/pennmobile/MainActivity.kt | 1 + .../pennapps/labs/pennmobile/NewsFragment.kt | 11 ++++++++--- .../pennapps/labs/pennmobile/SupportFragment.kt | 5 +++++ .../labs/pennmobile/api/StudentLife.java | 1 - 7 files changed, 20 insertions(+), 22 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 30d8c626..ed4a314e 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 @@ -48,10 +42,6 @@ class HomeFragment : Fragment() { mActivity = activity as MainActivity sharedPreferences = PreferenceManager.getDefaultSharedPreferences(mActivity) - - LocalBroadcastManager - .getInstance(mActivity) - .registerReceiver(broadcastReceiver, IntentFilter("refresh")) } override fun onCreateView( @@ -186,13 +176,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 72bb5349..ab906a80 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) diff --git a/PennMobile/src/main/java/com/pennapps/labs/pennmobile/NewsFragment.kt b/PennMobile/src/main/java/com/pennapps/labs/pennmobile/NewsFragment.kt index 47d7acfd..3dab6fff 100644 --- a/PennMobile/src/main/java/com/pennapps/labs/pennmobile/NewsFragment.kt +++ b/PennMobile/src/main/java/com/pennapps/labs/pennmobile/NewsFragment.kt @@ -21,7 +21,6 @@ import android.widget.ImageView import android.widget.ListView import android.widget.TextView import androidx.preference.PreferenceManager -import com.google.firebase.analytics.FirebaseAnalytics import com.pennapps.labs.pennmobile.databinding.FragmentNewsBinding import java.util.ArrayList @@ -41,6 +40,8 @@ class NewsFragment : ListFragment() { private var isCustomTabsSupported: Boolean = false private lateinit var sharedPreferences: SharedPreferences + private var connection: NewsCustomTabsServiceConnection? = null + private var _binding : FragmentNewsBinding? = null private val binding get() = _binding!! @@ -112,7 +113,7 @@ class NewsFragment : ListFragment() { override fun onActivityCreated(savedInstanceState: Bundle?) { super.onActivityCreated(savedInstanceState) - val connection = NewsCustomTabsServiceConnection() + connection = NewsCustomTabsServiceConnection() context?.let { context -> isCustomTabsSupported = isChromeCustomTabsSupported(context) } @@ -127,7 +128,7 @@ class NewsFragment : ListFragment() { androidx.appcompat.R.anim.abc_popup_enter, androidx.appcompat.R.anim.abc_popup_exit) CustomTabsClient.bindCustomTabsService(context, - CUSTOM_TAB_PACKAGE_NAME, connection) + CUSTOM_TAB_PACKAGE_NAME, connection!!) } addNews() @@ -228,5 +229,9 @@ class NewsFragment : ListFragment() { mActivity.removeTabs() super.onDestroyView() _binding = null + connection?.let { + context?.unbindService(connection!!) + } + } } \ No newline at end of file diff --git a/PennMobile/src/main/java/com/pennapps/labs/pennmobile/SupportFragment.kt b/PennMobile/src/main/java/com/pennapps/labs/pennmobile/SupportFragment.kt index c3517f27..3129461b 100644 --- a/PennMobile/src/main/java/com/pennapps/labs/pennmobile/SupportFragment.kt +++ b/PennMobile/src/main/java/com/pennapps/labs/pennmobile/SupportFragment.kt @@ -81,4 +81,9 @@ class SupportFragment : ListFragment() { mActivity.hideBottomBar() mActivity.setSelectedTab(MainActivity.MORE) } + + override fun onDestroyView() { + mActivity.toolbar.setNavigationOnClickListener(null) + super.onDestroyView() + } } \ No newline at end of file diff --git a/PennMobile/src/main/java/com/pennapps/labs/pennmobile/api/StudentLife.java b/PennMobile/src/main/java/com/pennapps/labs/pennmobile/api/StudentLife.java index a3d7e63e..15f3413e 100644 --- a/PennMobile/src/main/java/com/pennapps/labs/pennmobile/api/StudentLife.java +++ b/PennMobile/src/main/java/com/pennapps/labs/pennmobile/api/StudentLife.java @@ -15,7 +15,6 @@ import com.pennapps.labs.pennmobile.classes.GSRBookingResult; import com.pennapps.labs.pennmobile.classes.GSRLocation; import com.pennapps.labs.pennmobile.classes.GSRReservation; -import com.pennapps.labs.pennmobile.classes.Gym; import com.pennapps.labs.pennmobile.classes.LaundryRequest; import com.pennapps.labs.pennmobile.classes.LaundryRoom; import com.pennapps.labs.pennmobile.classes.LaundryRoomSimple;