From 762f0493ca7241eef1fd9f67f2d00f3ff25c9bc3 Mon Sep 17 00:00:00 2001 From: Chilun Date: Wed, 13 May 2020 17:20:09 +0800 Subject: [PATCH] Get the instance count as the initial value Developer may not be able to enable strict mode before creating any activities. The initial value of expected instance count may not always be 1. This CL use the instance count from InstanceTracker as the initial value. Bug: 152348723 Test: atest ActivityLeakTests atest NexusLauncherTests Change-Id: I8d34c87748deac72cf8b6e5cc247029901bbffb3 --- core/java/android/os/StrictMode.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/core/java/android/os/StrictMode.java b/core/java/android/os/StrictMode.java index 02b822a99f2ad..772845d4e6837 100644 --- a/core/java/android/os/StrictMode.java +++ b/core/java/android/os/StrictMode.java @@ -817,6 +817,9 @@ public final class StrictMode { /** @hide */ public @NonNull Builder permitActivityLeaks() { + synchronized (StrictMode.class) { + sExpectedActivityInstanceCount.clear(); + } return disable(DETECT_VM_ACTIVITY_LEAKS); } @@ -2586,8 +2589,10 @@ public final class StrictMode { return; } + // Use the instance count from InstanceTracker as initial value. Integer expected = sExpectedActivityInstanceCount.get(klass); - Integer newExpected = expected == null ? 1 : expected + 1; + Integer newExpected = + expected == null ? InstanceTracker.getInstanceCount(klass) + 1 : expected + 1; sExpectedActivityInstanceCount.put(klass, newExpected); } }