From fca0f15e161755e0a430802b1b38bdda0cb033cb Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Sun, 10 May 2015 22:23:45 +0300 Subject: Fix crash on closing sell dialog in some cases. --- src/gui/models/shopitems.cpp | 19 +++++++++++++++++++ src/gui/models/shopitems.h | 3 +++ src/gui/windows/npcselldialog.cpp | 3 +-- 3 files changed, 23 insertions(+), 2 deletions(-) (limited to 'src/gui') diff --git a/src/gui/models/shopitems.cpp b/src/gui/models/shopitems.cpp index 2fd399cb0..3a0d933cc 100644 --- a/src/gui/models/shopitems.cpp +++ b/src/gui/models/shopitems.cpp @@ -111,11 +111,27 @@ ShopItem *ShopItems::at(unsigned int i) const return mShopItems.at(i); } +bool ShopItems::findInAllItems(std::vector::iterator &it, + const ShopItem *const item) +{ + const std::vector::iterator it_end = mAllShopItems.end(); + for (it = mAllShopItems.begin(); it != it_end; ++ it) + { + if (*it == item) + return true; + } + return false; +} + void ShopItems::erase(const unsigned int i) { if (i >= static_cast(mShopItems.size())) return; + ShopItem *item = *(mShopItems.begin() + i); + std::vector::iterator it; + if (findInAllItems(it, item)) + mAllShopItems.erase(it); mShopItems.erase(mShopItems.begin() + i); } @@ -125,6 +141,9 @@ void ShopItems::del(const unsigned int i) return; ShopItem *item = *(mShopItems.begin() + i); + std::vector::iterator it; + if (findInAllItems(it, item)) + mAllShopItems.erase(it); mShopItems.erase(mShopItems.begin() + i); delete item; } diff --git a/src/gui/models/shopitems.h b/src/gui/models/shopitems.h index 50269cff1..b222f9385 100644 --- a/src/gui/models/shopitems.h +++ b/src/gui/models/shopitems.h @@ -147,6 +147,9 @@ class ShopItems final : public ListModel ShopItem *findItem(const int id, const unsigned char color) const A_WARN_UNUSED; + bool findInAllItems(std::vector::iterator &it, + const ShopItem *const item); + /** The list of items in the shop. */ std::vector mAllShopItems; diff --git a/src/gui/windows/npcselldialog.cpp b/src/gui/windows/npcselldialog.cpp index 881e15254..683593deb 100644 --- a/src/gui/windows/npcselldialog.cpp +++ b/src/gui/windows/npcselldialog.cpp @@ -101,8 +101,7 @@ void NpcSellDialog::sellAction(const ActionEvent &event) { // All were sold mShopItemList->setSelected(-1); - delete mShopItems->at(selectedItem); - mShopItems->erase(selectedItem); + mShopItems->del(selectedItem); Rect scroll; scroll.y = mShopItemList->getRowHeight() * (selectedItem + 1); -- cgit v1.2.3-60-g2f50