Service has leaked ServiceConnection that was originally bound here

HiddenDroid picture HiddenDroid · Jul 8, 2015 · Viewed 21.8k times · Source

I am using a BraodCastReceiver that starts an IntentService. Everythings look working good, but I get this error which I don't know its source:

android.app.ServiceConnectionLeaked: Service     com.merchantech.app.smartdiscount.MyIntentService has leaked ServiceConnection  com.clover.sdk.v3.order.OrderConnector@535008f4 that was originally bound here
        at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:969)
        at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863)
        at android.app.ContextImpl.bindService(ContextImpl.java:1426)
        at android.app.ContextImpl.bindService(ContextImpl.java:1415)
        at android.content.ContextWrapper.bindService(ContextWrapper.java:473)
        at com.clover.sdk.v1.ServiceConnector.connect(ServiceConnector.java:119)
        at com.clover.sdk.v1.ServiceConnector.waitForConnection(ServiceConnector.java:148)
        at com.clover.sdk.v1.ServiceConnector.execute(ServiceConnector.java:209)
        at com.clover.sdk.v3.order.OrderConnector.getOrder(OrderConnector.java:153)
        at com.merchantech.app.smartdiscount.MyIntentService.onHandleIntent(MyIntentService.java:41)
        at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.os.HandlerThread.run(HandlerThread.java:60)

Here's my BrodcastReceiver class:

public class MyReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (action.equals("com.test.intent.action.LINE_ITEM_ADDED")) {
            Intent i = new Intent(context, MyIntentService.class);
            context.startService(i);
        }
    }
}

And here's my IntentService class:

public class MyIntentService extends IntentService {

    public MyIntentService() {
        super("MyIntentService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        orderItem.addLineItem(orderId, lineItemId, var);
    }
}

Answer

Bob Snyder picture Bob Snyder · Jul 8, 2015

The error message basically means that some component created a binding to a Service and did not unbind from the service before the component was destroyed.

You will need to provide more information about your app's structure to get a better answer to your question. I'll make some guesses, and maybe give you some ideas about things to explore.

I'm guessing that you are using a library from Clover and maybe also Merchantech. Or maybe the Clover library uses a Merchantech library. I'm also guessing that orderItem is an instance of a class defined by Clover, not you.

The Android runtime destroys an IntentService after onHandleIntent() completes and there are no more Intent requests queued. You can confirm this by adding the onDestroy() method to your MyIntentService with a Log command to show when it is called. That means that in the code you posted for onHandleIntent(), your IntentService will be destroyed soon after the call to addLineItem() completes.

The stack trace suggests that the processing triggered by the call to orderItem.addLineItem() causes a binding to another service. Somewhere, that binding is not being managed properly--maybe not unbound when it should be. Is there something the Clover subsystem expects you do to "close" it or "release" resources when your component (Service or Activity) is being destroyed?