Spot the bug
LeakCanary reports MainActivity leaked through AnalyticsTracker.listener. Which line causes it?
object AnalyticsTracker {var listener: (() -> Unit)? = null}class MainActivity : ComponentActivity() {override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)AnalyticsTracker.listener = { title = "Synced" }}}
Hint 1 of 1
What does the lambda capture?
Choose an answer, then open Show answer below to check it.
Show answer
- Answer: line 8
AnalyticsTracker.listener = { title = "Synced" }
The lambda captures the Activity (it touches title) and the singleton keeps it forever. Clear the listener in onDestroy, or expose a Flow the Activity collects with its lifecycle.