Clear binder stub references to surrounding objects
The binder stubs are GC roots and may be kept for longer than
the surrounding MediaSessionRecord. This is unavoidable as apps
can keep references to MediaControllers, MediaSessions, or
MediaSession.Tokens, all of which have a binder reference to one
of the two stubs in MediaSessionRecord and no explicit way to
remotely clear this reference from the system server (especially
for the Token and Controller).
To avoid memory leaks, we can ensure the MediaSessionRecord is
disconnected from these stubs and can be garbage-collected.
Changes done in this CL:
- Make both stubs static and keep a WeakReference to the surrounding
MediaSessionRecord only. This reference can be cleared when
the MediaSessionRecord is destroyed, but using a WeakReference
also guards against keeping a reference if the MediaSessionRecord
is garbage collected without explicit calls to destroy.
- The actual implementation of all stub methods moved to
MediaSessionRecord.
- The stubs forward to the corresponding MediaSessionRecord method
if the reference is still valid. Otherwise:
- Calls without return value can be ignored (but logged).
- Other calls must return a placeholder value since
the MediaController instances are not throwing at the moment
and we don't intent to change the API contract.
BYPASS_LARGE_CHANGE_WARNING - bulk moving existing code
Bug: 261882243
Flag: EXEMPT bugfix
Test: existing tests, new tests for no throw after release, manual verification leak in b/261882243 is gone
Change-Id: I924c67aa31faecd191e3f84896dc5094b64d4011
2 files changed