From 3f06d6c9902f99e3ea34f6c8e1a9f1df9915eb6e Mon Sep 17 00:00:00 2001 From: momo5502 Date: Sun, 9 Jul 2017 01:56:12 +0200 Subject: [PATCH] [Menus] Safer menu merging --- src/Components/Modules/Menus.cpp | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/Components/Modules/Menus.cpp b/src/Components/Modules/Menus.cpp index f76df9d7..31be3ea0 100644 --- a/src/Components/Modules/Menus.cpp +++ b/src/Components/Modules/Menus.cpp @@ -273,17 +273,17 @@ namespace Components void Menus::SafeMergeMenus(std::vector>* menus, std::vector> newMenus) { // Check if we overwrote a menu - for (unsigned int i = 0; i < menus->size(); ++i) + for (auto i = menus->begin(); i != menus->end();) { // Try to find the native menu - bool found = !menus->at(i).first; // Only if custom menu, try to find it + bool found = !i->first; // Only if custom menu, try to find it // If there is none, try to find a custom menu if (!found) { for (auto& entry : Menus::MenuList) { - if (menus->at(i).second == entry.second) + if (i->second == entry.second) { found = true; break; @@ -294,23 +294,26 @@ namespace Components // Remove the menu if it has been deallocated (not found) if (!found) { - menus->erase(menus->begin() + i); - --i; + i = menus->erase(i); continue; } + bool increment = true; + // Remove the menu if it has been loaded twice for (auto& newMenu : newMenus) { - if (menus->at(i).second->window.name == std::string(newMenu.second->window.name)) + if (i->second->window.name == std::string(newMenu.second->window.name)) { - Menus::RemoveMenu(menus->at(i).second); + Menus::RemoveMenu(i->second); - menus->erase(menus->begin() + i); - --i; + i = menus->erase(i); + increment = false; break; } } + + if(increment) ++i; } Utils::Merge(menus, newMenus);