diff --git a/src/openrct2-ui/WindowManager.cpp b/src/openrct2-ui/WindowManager.cpp index 1f2ba6354b..1fd1e137c5 100644 --- a/src/openrct2-ui/WindowManager.cpp +++ b/src/openrct2-ui/WindowManager.cpp @@ -1321,14 +1321,22 @@ public: for (auto it = g_window_list.rbegin(); it != g_window_list.rend(); it++) { auto& w2 = *it; + if (w2->flags & WF_DEAD) + { + continue; + } if (!(w2->flags & WF_STICK_TO_FRONT)) { - itDestPos = it.base(); + // base() returns the next element in the list, so we need to decrement it. + itDestPos = std::prev(it.base()); break; } } - std::iter_swap(itSourcePos, itDestPos); + if (itSourcePos != itDestPos) + { + std::iter_swap(itSourcePos, itDestPos); + } w.Invalidate(); if (w.windowPos.x + w.width < 20)