Skip to content

Commit b5a8654

Browse files
Johannes CarlssonDianne Hackborn
authored andcommitted
Clear reference to the IIntentReceiver in order to avoid memory leak
When using sendOrderedBroadcast(..) with a BroadcastReceiver the BroadcastReceiver instance was not released. The reason for this was that the resultTo field in the BroadcastRecord kept a reference until it was pushed out of the mBroadcastHistory. This reference in turn kept a reference to the process side IIntentReceiver (implemented in ReceiverDispatcher$InnerReceiver). This in turn had a strong reference (through mStrongRef) to the Context. In order to keep the debug output the resultTo is also kept as a String in the new resultToString variable. Change-Id: I4382a22a541c27b3694fb2b78a04ee820b235f8f
1 parent c91f9fe commit b5a8654

File tree

2 files changed

+4
-1
lines changed

2 files changed

+4
-1
lines changed

services/java/com/android/server/am/ActivityManagerService.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11009,6 +11009,9 @@ private final void processNextBroadcast(boolean fromMsg) {
1100911009
performReceiveLocked(r.callerApp, r.resultTo,
1101011010
new Intent(r.intent), r.resultCode,
1101111011
r.resultData, r.resultExtras, false, false);
11012+
// Set this to null so that the reference
11013+
// (local and remote) isnt kept in the mBroadcastHistory.
11014+
r.resultTo = null;
1101211015
} catch (RemoteException e) {
1101311016
Slog.w(TAG, "Failure sending broadcast result of " + r.intent, e);
1101411017
}

services/java/com/android/server/am/BroadcastRecord.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class BroadcastRecord extends Binder {
4545
final boolean initialSticky; // initial broadcast from register to sticky?
4646
final String requiredPermission; // a permission the caller has required
4747
final List receivers; // contains BroadcastFilter and ResolveInfo
48-
final IIntentReceiver resultTo; // who receives final result if non-null
48+
IIntentReceiver resultTo; // who receives final result if non-null
4949
long dispatchTime; // when dispatch started on this set of receivers
5050
long receiverTime; // when current receiver started for timeouts.
5151
long finishTime; // when we finished the broadcast.

0 commit comments

Comments
 (0)