Fix issue #3344089: runtime restart due to java.lang.StackOverflowError...

...in WindowManager after app force closed a few times

Change-Id: I9d5ff16b01d14030150ba0904a47635f9c7a158f
diff --git a/services/java/com/android/server/WindowManagerService.java b/services/java/com/android/server/WindowManagerService.java
index 7011f9a..7b7a891 100644
--- a/services/java/com/android/server/WindowManagerService.java
+++ b/services/java/com/android/server/WindowManagerService.java
@@ -9216,27 +9216,32 @@
             return;
         }
 
+        mInLayout = true;
         boolean recoveringMemory = false;
-        if (mForceRemoves != null) {
-            recoveringMemory = true;
-            // Wait a little it for things to settle down, and off we go.
-            for (int i=0; i<mForceRemoves.size(); i++) {
-                WindowState ws = mForceRemoves.get(i);
-                Slog.i(TAG, "Force removing: " + ws);
-                removeWindowInnerLocked(ws.mSession, ws);
-            }
-            mForceRemoves = null;
-            Slog.w(TAG, "Due to memory failure, waiting a bit for next layout");
-            Object tmp = new Object();
-            synchronized (tmp) {
-                try {
-                    tmp.wait(250);
-                } catch (InterruptedException e) {
+        
+        try {
+            if (mForceRemoves != null) {
+                recoveringMemory = true;
+                // Wait a little it for things to settle down, and off we go.
+                for (int i=0; i<mForceRemoves.size(); i++) {
+                    WindowState ws = mForceRemoves.get(i);
+                    Slog.i(TAG, "Force removing: " + ws);
+                    removeWindowInnerLocked(ws.mSession, ws);
+                }
+                mForceRemoves = null;
+                Slog.w(TAG, "Due to memory failure, waiting a bit for next layout");
+                Object tmp = new Object();
+                synchronized (tmp) {
+                    try {
+                        tmp.wait(250);
+                    } catch (InterruptedException e) {
+                    }
                 }
             }
+        } catch (RuntimeException e) {
+            Slog.e(TAG, "Unhandled exception while force removing for memory", e);
         }
-
-        mInLayout = true;
+        
         try {
             performLayoutAndPlaceSurfacesLockedInner(recoveringMemory);