From ea33d792f6fbc22a4ae906c6973860e4fbe138e2 Mon Sep 17 00:00:00 2001 From: michabr <82236305+michabr@users.noreply.github.com> Date: Wed, 8 Dec 2021 15:53:16 -0800 Subject: [PATCH] Fix assert in LyShine when unloading a level (#6260) * Fix assert in LyShine when unloading a level Signed-off-by: abrmich * Remove use of iter now that it's no longer used Signed-off-by: abrmich --- Gems/LyShine/Code/Source/UiCanvasManager.cpp | 22 ++++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/Gems/LyShine/Code/Source/UiCanvasManager.cpp b/Gems/LyShine/Code/Source/UiCanvasManager.cpp index 3bf0263363..62d1c6e2a8 100644 --- a/Gems/LyShine/Code/Source/UiCanvasManager.cpp +++ b/Gems/LyShine/Code/Source/UiCanvasManager.cpp @@ -629,23 +629,23 @@ void UiCanvasManager::RenderLoadedCanvases() //////////////////////////////////////////////////////////////////////////////////////////////////// void UiCanvasManager::DestroyLoadedCanvases(bool keepCrossLevelCanvases) { - // Delete all the canvases loaded in game (but not loaded in editor) - for (auto iter = m_loadedCanvases.begin(); iter != m_loadedCanvases.end(); ++iter) + // Find all the canvases loaded in game (but not loaded in editor) that need destroying + AZStd::vector canvasesToUnload; + canvasesToUnload.reserve(m_loadedCanvases.size()); + for (auto canvas : m_loadedCanvases) { - auto canvas = *iter; - if (!(keepCrossLevelCanvases && canvas->GetKeepLoadedOnLevelUnload())) { - // no longer used by game so delete the canvas - delete canvas->GetEntity(); - *iter = nullptr; // mark for removal from container + canvasesToUnload.push_back(canvas->GetEntityId()); } } - // now remove the nullptr entries - m_loadedCanvases.erase( - std::remove(m_loadedCanvases.begin(), m_loadedCanvases.end(), nullptr), - m_loadedCanvases.end()); + // Unload the canvases. This will also send the OnCanvasUnloaded notification which + // ensures that components such as UiCanvasAsserRefComponent can clean up properly + for (auto canvasEntityId : canvasesToUnload) + { + UnloadCanvas(canvasEntityId); + } } ////////////////////////////////////////////////////////////////////////////////////////////////////