Teovald
Teovald

Reputation: 4389

What does it mean to leak a service connection?

I am writing a service for my Android app and I am trying to understand how the binding mechanism works. If I bind my service in the onCreate of an activity but I don't unbind it in onStop or onDestroy, I get the error :

android.app.ServiceConnectionLeaked: Service com.google.ipc.invalidation.ticl.android.AndroidInvalidationService has leaked ServiceConnection com.googl
                              e.ipc.invalidation.external.client.android.service.ServiceBinder$1@4177f8f8 that was originally bound here

So my question is : what is the problem exactly with leaking a connection, what am I preventing by unbinding my service ?

Upvotes: 20

Views: 32873

Answers (3)

Mohammad Ersan
Mohammad Ersan

Reputation: 12444

This issue occurs when the Android OS destroys your Activity while a ServiceConnection is still bound to a running Service. To prevent this, you need to unbind the service before the Activity is destroyed.

Upvotes: 42

Vishruit Kulshreshtha
Vishruit Kulshreshtha

Reputation: 198

The services are still running even after the activity gets closed. So you need to unbind the resource in onStop or onDestroy.

Upvotes: 4

Sam
Sam

Reputation: 86948

Typically your app is the only app that can access your Service (or have a use for it). If destroy your app with this Service still running then that Service no longer has a purpose. It will exist in memory consuming resources but doing nothing. By unbinding or stopping your Service when you are done, you return these resources so that they can be used by other apps.

Think of a child that spreads their toys across a room and then leaves. Everyone else in the house (the parents, siblings and guests) has no use for them and must walk around everything. The toys are simply in the way. If the child puts them away, then anyone can use the room for whatever they want.

Hope that helps!

Upvotes: 48

Related Questions