BillingBroadcastReceiver Memory Leak
See original GitHub issueAfter buying a item successfully, I am closing the current activity which holds purching process. But Leak Canary catch a memory leak about BillingBroadcastReceiver
. I init billing client OnCreate
and release onDestroy
.
Here is my init method
billingClient = BillingClient.newBuilder(this).setListener(this).build();
billingClient.startConnection(new BillingClientStateListener() {
@Override
public void onBillingSetupFinished(int responseCode) {
if (responseCode == BillingClient.BillingResponse.OK) {
// The billing client is ready. You can query purchases here.
loadProducts();
} else {
// Error
}
}
@Override
public void onBillingServiceDisconnected() {
// Try to restart the connection on the next request to
Timber.d("Connection Error");
}
});
Here is my release Method
if (billingClient != null && billingClient.isReady()) {
billingClient.endConnection();
billingClient = null;
}
OnPurchaseUpdated I made a service call and close this activity based on service result.
public void onPurchasesUpdated(int responseCode, @Nullable List<Purchase> purchases) {
if (responseCode == BillingClient.BillingResponse.OK && purchases != null) {
for (Purchase purchase : purchases) {
billingClient.consumeAsync(purchase.getPurchaseToken(), new ConsumeResponseListener() {
@Override
public void onConsumeResponse(int responseCode, String purchaseToken) {
if (responseCode == BillingClient.BillingResponse.OK && purchaseToken != null) {
Timber.d("onConsumeResponse --> %s", purchaseToken);
getViewModel().informPurchase(necessary data);
}
}
});
}
} else if (responseCode == BillingClient.BillingResponse.USER_CANCELED) {
// Handle an error caused by a user canceling the purchase flow.
Timber.d("Billing Cancelled");
} else {
Timber.d("An Error Occured");
}
}
Here is the Leak Canary error
Issue Analytics
- State:
- Created 5 years ago
- Reactions:1
- Comments:23 (1 by maintainers)
Top Results From Across the Web
In App Billing BroadcastReceiver Memory Leak - Stack Overflow
I am opening a Activity with ActivityResult and after buying a item successfully, I am closing the current activity which holds purching processΒ ......
Read more >In App Billing Broadcastreceiver Memory Leak - ADocLib
secure applications that do not leak or alter user data in the presence of an adversary. another Activity that prompts the user for...
Read more >Gist to demonstrate memory leak when using Broadcast ...
* You need to unregister the broadcast receiver since the broadcast receiver keeps a reference of the activity. * is still holding a...
Read more >Broadcasts overview | Android Developers
Android apps can send or receive broadcast messages from the Android system and other Android apps, similar to the publish-subscribe design pattern.
Read more >Memory Leaks in details in Android | by SHISHIR - Medium
A memory leak happens when memory is allocated but never freed. This means the garbage collector is not able to take out the...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
With billingClient 4.1.0 still have a similar leak. Looks like
billingClient.endConnection()
doesnβt destroy some Handler with a reference to some listenerI believe we found the issue: PBL is holding an activity context and didnβt free it. The issue has been fixed. It shall be rolled out with the next PBL release.
On Thu, Jan 7, 2021 at 2:29 AM haris15 notifications@github.com wrote: