From f7abcda5f22cde86666aeedbf1cc2a99b47ec2c2 Mon Sep 17 00:00:00 2001 From: Gopal Krishna Shukla Date: Wed, 6 Jul 2016 08:14:59 +0530 Subject: [PATCH] Provide synchronization to setview to avoid NPE If setView() will be called from two different threads then mView property of a View object may have inconsistent value. For instance, setView() may set mView to null causing NullPointerException. Synchronize root.setView() as well to avoid this. Change-Id: I5f9cf47ece5d4aca575bd8644ecfcee0ed43d843 --- core/java/android/view/WindowManagerGlobal.java | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/core/java/android/view/WindowManagerGlobal.java b/core/java/android/view/WindowManagerGlobal.java index 11734d31388e9..830df990f2053 100644 --- a/core/java/android/view/WindowManagerGlobal.java +++ b/core/java/android/view/WindowManagerGlobal.java @@ -335,20 +335,17 @@ public final class WindowManagerGlobal { mViews.add(view); mRoots.add(root); mParams.add(wparams); - } - // do this last because it fires off messages to start doing things - try { - root.setView(view, wparams, panelParentView); - } catch (RuntimeException e) { - // BadTokenException or InvalidDisplayException, clean up. - synchronized (mLock) { - final int index = findViewLocked(view, false); + // do this last because it fires off messages to start doing things + try { + root.setView(view, wparams, panelParentView); + } catch (RuntimeException e) { + // BadTokenException or InvalidDisplayException, clean up. if (index >= 0) { removeViewLocked(index, true); } + throw e; } - throw e; } }