Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
- }
- }
- 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 onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- bindService(Intent(this, LocalService::class.java), serviceConnection, BIND_AUTO_START)
- }
- override fun onDestroy() {
- service?.let {
- unbindService(serviceConnection)
- service = null
- }
- super.onDestroy()
- }
- }
- ┬
- ├─ 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)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement