SF: Avoid accessing an invalidated iterator

If we erase the element currently pointed to by
the iterator, the iterator will be invalidated.
Fix this by updating the iterator.

Bug: 238781169
Test: presubmit
Change-Id: I5306cb15f7665f53475ce2fa3b5ef6a897f73dca
diff --git a/services/surfaceflinger/FrontEnd/LayerLifecycleManager.cpp b/services/surfaceflinger/FrontEnd/LayerLifecycleManager.cpp
index 1108246..7afa144 100644
--- a/services/surfaceflinger/FrontEnd/LayerLifecycleManager.cpp
+++ b/services/surfaceflinger/FrontEnd/LayerLifecycleManager.cpp
@@ -123,7 +123,11 @@
             ALOGV("%s destroyed layer %s", __func__, layer->getDebugStringShort().c_str());
             std::iter_swap(it, mLayers.end() - 1);
             mDestroyedLayers.emplace_back(std::move(mLayers.back()));
-            mLayers.erase(mLayers.end() - 1);
+            if (it == mLayers.end() - 1) {
+                it = mLayers.erase(mLayers.end() - 1);
+            } else {
+                mLayers.erase(mLayers.end() - 1);
+            }
         } else {
             it++;
         }