From 56ac2e247553107edb99d243dfbd13284ce25cb6 Mon Sep 17 00:00:00 2001 From: Beverly Date: Fri, 1 Jul 2022 16:22:24 +0000 Subject: Make sure to cleanup views that couldn't be added ie: due to BadTokenException or InvalidDisplayException Previously, only views that were already in the viewhierarchy before attempted to be re-added would be removed. This makes sure if the view was newly added, it'll also be removed. This prevents a memory leak of views. Test: manually show and cancel multiple toasts, check the hierachy viewer that no Toast views remain Fixes: 234694098 Change-Id: I06bbae70c277d0615753edc9ec0a8e7439ad7020 --- core/java/android/view/WindowManagerGlobal.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/core/java/android/view/WindowManagerGlobal.java b/core/java/android/view/WindowManagerGlobal.java index 25445abefca2..ac4c2ea448ac 100644 --- a/core/java/android/view/WindowManagerGlobal.java +++ b/core/java/android/view/WindowManagerGlobal.java @@ -404,9 +404,10 @@ public final class WindowManagerGlobal { try { root.setView(view, wparams, panelParentView, userId); } catch (RuntimeException e) { + final int viewIndex = findViewLocked(view, false); // BadTokenException or InvalidDisplayException, clean up. - if (index >= 0) { - removeViewLocked(index, true); + if (viewIndex >= 0) { + removeViewLocked(viewIndex, true); } throw e; } -- cgit v1.2.3-59-g8ed1b