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?
Context#bindService()
call without a correspondingContext#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 though – TyronetyrosinasemLicenseCheckerCallback
andmChecker
to null inonPause()
– Coprophilia