Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use strong references to callbacks in Stripe and CustomerSession #863

Merged
merged 1 commit into from Apr 11, 2019

Conversation

mshafrir-stripe
Copy link
Collaborator

Summary

Stripe and CustomerSession methods accept callback objects
that typically hold references to Activity objects. The best
practice in Android is that references to Activity objects
should be weakly held for long-running background tasks, because
the Activity may go away (e.g. user leaves activity before task
is complete). The change was first introduced in #666.

After this change, users began reporting that callback objects
would mysteriously be garbage-collected by the OS. I was able
to reproduce this as well, and found a work-around by moving
anonymous inline classes to instance variables - see 2b74328.

Android will GC objects that are no longer strongly referenced.
When inlined, the callback objects were no longer strongly
referenced once the method completed (e.g. in 2b74328, the
left-side of AddSourceActivity#onActionSave); moving these
objects to be instance variables meant that they were now
strongly-referenced by the Activity. This explains why the GC
issues were resolved with this change.

In conclusion, the correct solution is to have the callback
objects weakly-reference Activity objects, rather than
weakly-reference the callback objects themselves.

Motivation

ANDROID-340

Testing

Manually tested

**Summary**
`Stripe` and `CustomerSession` methods accept callback objects
that typically hold references to `Activity` objects. The best
practice in Android is that references to `Activity` objects
should be weakly held for long-running background tasks, because
the `Activity` may go away (e.g. user leaves activity before task
is complete). The change was first introduced in #666.

After this change, users began reporting that callback objects
would mysteriously be garbage-collected by the OS. I was able
to reproduce this as well, and found a work-around by moving
anonymous inline classes to instance variables - see 2b74328.

Android will GC objects that are no longer strongly referenced.
When inlined, the callback objects were no longer strongly
referenced once the method completed (e.g. in 2b74328, the
left-side of `AddSourceActivity#onActionSave`); moving these
objects to be instance variables meant that they were now
strongly-referenced by the Activity. This explains why the GC
issues were resolved with this change.

In conclusion, the correct solution is to have the callback
objects weakly-reference `Activity` objects, rather than
weakly-reference the callback objects themselves.

**Motivation**
ANDROID-340
@mshafrir-stripe mshafrir-stripe merged commit 9804bcd into master Apr 11, 2019
@mshafrir-stripe mshafrir-stripe deleted the fix-weak-refs branch April 11, 2019 20:45
@jemerick-stripe jemerick-stripe mentioned this pull request Apr 12, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants