I wrote a basic bound service based on the Android documentation, but LeakCanary is telling me the service is leaking.
- Is there a leak or have I misconfigured LeakCanary?
- How can I write a bound service that does not leak?
The Code
class LocalService : Service() {
private val binder = LocalBinder()
private val generator = Random()
val randomNumber: Int
get() = generator.nextInt(100)
inner class LocalBinder : Binder() {
fun getService(): LocalService = this@LocalService
}
override fun onBind(intent: Intent): IBinder {
return binder
}
override fun onDestroy() {
super.onDestroy()
LeakSentry.refWatcher.watch(this) // Only modification is to add LeakCanary
}
}
If I bind to the service from an activity as follows, LeakCanary detects the service has leaked
class MainActivity: Activity() {
private var service: LocalService? = null
private val serviceConnection = object: ServiceConnection {
override fun onServiceConnected(name: ComponentName?, binder: IBinder?) {
service = (binder as LocalBinder).getService()
}
override fun onServiceDisconnected(name: ComponentName?) {
service = null
}
}
override fun onStart() {
super.onStart()
bindService(Intent(this, LocalService::class.java), serviceConnection, BIND_AUTO_CREATE)
}
override fun onStop() {
super.onStop()
service?.let {
unbindService(serviceConnection)
service = null
}
}
}
┬
├─ com.example.serviceleak.LocalService$LocalBinder
│ Leaking: NO (it's a GC root)
│ ↓ LocalService$LocalBinder.this$0
│ ~~~~~~
╰→ com.example.serviceleak.LocalService
Leaking: YES (RefWatcher was watching this)
BIND_AUTO_CREATE
. Whether the service is bound and unbound in onCreate and onDestroy is irrelevant; It leaks no matter which lifecycle methods you use to bind and unbind. But I'll change my example to match the Android docs. – Organogenesis