LicenseChecker checkAccess leaks ServiceConnection
Asked Answered
S

4

19

I am receiving this exception in LogCat every time I press the Back button in my app:

Activity has leaked ServiceConnection com.android.vending.licensing.LicenseChecker@471cc039 that was originally bound here

The code responsible for this leak in onCreate() is:

mLicenseCheckerCallback = new MyLicenseCheckerCallback();
mChecker.checkAccess(mLicenseCheckerCallback);

How do I get rid of this leak?

I tried not assigning MyLicenseCheckerCallback to a member, thinking perhaps when the activity goes onPause() the reference to the callback is responsible for the leak:

mChecker.checkAccess(new MyLicenseCheckerCallback());

But that didn't get rid of the leak.

Update: Thanks to @zapl's comment below, I looked at Google's LicenseChecker.java:

/** Unbinds service if necessary and removes reference to it. */
private void cleanupService() {
    if (mService != null) {
        try {
            mContext.unbindService(this);
        } catch (IllegalArgumentException e) {
            // Somehow we've already been unbound. This is a non-fatal error.
            Log.e(TAG, "Unable to unbind from licensing service (already unbound)");
        }
        mService = null;
    }
}

At first I thought that I may be neglecting to call it, but I double-checked and I am calling mChecker.onDestroy(); in my activity's onDestroy().

I also checked onDestroy() in LicenseChecker.java and it is calling unbindService:

/**
 * Inform the library that the context is about to be destroyed, so that
 * any open connections can be cleaned up.
 * <p>
 * Failure to call this method can result in a crash under certain
 * circumstances, such as during screen rotation if an Activity requests
 * the license check or when the user exits the application.
 */
public synchronized void onDestroy() {
    cleanupService();
    mHandler.getLooper().quit();
}

So, what is really going on?

Is this a bug in LVL?

Stimulate answered 16/8, 2012 at 17:58 Comment(3)
There is some Context#bindService() call without a corresponding Context#unbindService(). You (or the code you are using) have to unbind the service before the activity instance is destroyed to prevent that message. It won't crash your app if you leak the connection thoughTyronetyrosinase
@Tyronetyrosinase Thanks. Yes, I know that app doesn't crash but it bothers me to have something like this in my code. The problem is that I didn't create this service, this is a Google-supplied service.Stimulate
Have you tried setting mLicenseCheckerCallback and mChecker to null in onPause()Coprophilia
T
6

I just got the same problem, and with your update and zapl's comment I figured up that the problem is the emulator you are using.

This Emulators don't have the Google Play APIs, and the LVL can't bind to the service, leaving a connection open, at the end LVL can't close it with the onDestroy call.

Just create a new AVD using Google APIs instead of Android x.x and try your code there, if you don´t find the Google APIs in the Target pulldown when creating the new AVD download it with the Android SDK Manager.

Tardiff answered 23/10, 2013 at 7:16 Comment(1)
That was my problem on Huawei P40.Finegrain
H
4

I have also met the same problem later I got to know that i havn't added that android permission com.android.vending.CHECK_LICENSE . After correcting this my was problem is now solved. Try adding this line your android manifest

<uses-permission android:name="com.android.vending.CHECK_LICENSE" />
Hundredth answered 15/12, 2012 at 13:19 Comment(1)
And make sure you spell LICENSE correctly. With an S not a C. Like I did. DOH!Opinicus
P
0

Just put

mChecker.onDestroy();

on your onDestroymethod of the activity that declares and uses the mChecker.

While Google's code in LicenceChecker looks like this:

  public synchronized void onDestroy() {
        cleanupService();
        mHandler.getLooper().quit();
    }
Parliamentarianism answered 1/10, 2012 at 21:35 Comment(1)
Did you read my post? "I double-checked and I am calling mChecker.onDestroy(); in my activity's onDestroy()."Stimulate
S
0

I don't know about google's LicenceChecker, but you should call StopService() before exit the Activity otherwise the service is still running and leaks memory.

Subdue answered 15/12, 2012 at 20:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.