Skip to content

Commit 0a8aadd

Browse files
committed
bugfix: fix DebugPanelBroadcastReceiver memory leak
1 parent 1dc4bf9 commit 0a8aadd

3 files changed

Lines changed: 6 additions & 10 deletions

File tree

panel-core/src/main/kotlin/com/redmadrobot/debug/core/DebugPanelInstance.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import kotlinx.coroutines.channels.BufferOverflow
1111
import kotlinx.coroutines.flow.Flow
1212
import kotlinx.coroutines.flow.MutableSharedFlow
1313

14-
internal class DebugPanelInstance constructor(
14+
internal class DebugPanelInstance(
1515
application: Application,
1616
plugins: List<Plugin> = emptyList()
1717
) {

panel-core/src/main/kotlin/com/redmadrobot/debug/core/util/DebugPanelBroadcastReceiver.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,18 @@ import android.content.BroadcastReceiver
55
import android.content.Context
66
import android.content.Intent
77
import com.redmadrobot.debug.core.DebugPanel
8+
import java.lang.ref.WeakReference
89

9-
internal class DebugPanelBroadcastReceiver(
10-
private val activity: Activity
11-
) : BroadcastReceiver() {
10+
internal class DebugPanelBroadcastReceiver(activity: Activity) : BroadcastReceiver() {
11+
private val activityReference = WeakReference(activity)
1212

1313
companion object {
1414
const val ACTION_OPEN_DEBUG_PANEL = "com.redmadrobot.debug.core.ACTION_OPEN_DEBUG_PANEL"
1515
}
1616

1717
override fun onReceive(context: Context, intent: Intent) {
1818
if (intent.action == ACTION_OPEN_DEBUG_PANEL) {
19-
DebugPanel.showPanel(activity)
19+
activityReference.get()?.let { DebugPanel.showPanel(it) }
2020
}
2121
}
2222
}

sample/src/main/kotlin/com/redmadrobot/debug_sample/MainActivity.kt

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@ import com.redmadrobot.debug_sample.network.ApiFactory
1414
import com.redmadrobot.debugpanel.databinding.ActivityMainBinding
1515
import com.redmadrobot.flipper.config.FlipperValue
1616
import kotlinx.coroutines.DelicateCoroutinesApi
17-
import kotlinx.coroutines.Dispatchers
18-
import kotlinx.coroutines.GlobalScope
19-
import kotlinx.coroutines.flow.flowOn
2017
import kotlinx.coroutines.flow.launchIn
2118
import kotlinx.coroutines.flow.onEach
2219
import okhttp3.ResponseBody
@@ -146,8 +143,7 @@ class MainActivity : AppCompatActivity() {
146143
.onEach { changedToggles ->
147144
onFlipperTogglesChanged(changedToggles)
148145
}
149-
.flowOn(Dispatchers.Main)
150-
.launchIn(GlobalScope)
146+
.launchIn(lifecycleScope)
151147
}
152148

153149
private fun onFlipperTogglesChanged(changedToggles: Map<String, FlipperValue>) {

0 commit comments

Comments
 (0)