Merge pull request #81 from QuasarApp/task_80

Fix crash of switch between levels
This commit is contained in:
Andrei Yankovich 2021-07-06 20:43:14 +03:00 committed by GitHub
commit af8e7f044a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 11 deletions

View File

@ -165,10 +165,10 @@ bool IWorld::prepare() {
}
void IWorld::clearItems() {
QMutexLocker lock(&_ItemsMutex);
stop();
for (const auto& item : qAsConst(_items)) {
delete item;
while (_items.cbegin() != _items.cend()) {
removeItem(*_items.cbegin());
}
_items.clear();
@ -202,16 +202,18 @@ void IWorld::addItem(IWorldItem *obj, QList<int> *addedObjectsList) {
}
void IWorld::removeItem(int id, QList<int> *removedObjectsList) {
removeItem(getItem(id), removedObjectsList);
}
auto obj = getItem(id);
void IWorld::removeItem(IWorldItem* item, QList<int> *removedObjectsList) {
if (!obj)
if (!item)
return;
Diff diff;
// Work wih claster
if (auto claster = dynamic_cast<Claster*>(obj)) {
if (auto claster = dynamic_cast<Claster*>(item)) {
const auto copyOfObjectsList = claster->objects();
for (auto item : copyOfObjectsList) {
if (!item || item->parentClastersCount() > 1)
@ -226,8 +228,8 @@ void IWorld::removeItem(int id, QList<int> *removedObjectsList) {
}
}
removeAtomicItem(obj);
diff.removeIds.push_back(id);
removeAtomicItem(item);
diff.removeIds.push_back(item->guiId());
if (removedObjectsList)
*removedObjectsList = diff.removeIds;
@ -238,7 +240,6 @@ void IWorld::removeItem(int id, QList<int> *removedObjectsList) {
void IWorld::reset() {
if (_player) {
delete _player;
_player = nullptr;
}
@ -287,7 +288,7 @@ bool IWorld::removeAtomicItem(int id) {
_itemsGroup.remove(obj->className(), id);
_items.remove(id);
delete obj;
obj->deleteLater();
return true;
}
@ -302,7 +303,7 @@ bool IWorld::removeAtomicItem(IWorldItem *obj) {
_itemsGroup.remove(obj->className(), obj->guiId());
_items.remove(obj->guiId());
delete obj;
obj->deleteLater();
return true;
}

View File

@ -419,10 +419,19 @@ private:
* @brief removeItem This method remove item from the world. If the @a id are id of the claster object then its child object will be removed too.
* @param id This is id of removed object.
* @param removedObjectsList This is list of removed objects. Leave this argument nullptr for ignore this argument.
* @note this is wrapper of the removeItem(IWorldItem*, QList<int>*);
*/
void removeItem(int id,
QList<int>* removedObjectsList = nullptr);
/**
* @brief removeItem This method remove item from the world. If the @a id are id of the claster object then its child object will be removed too.
* @param item This is object than will be removed.
* @param removedObjectsList This is list of removed objects. Leave this argument nullptr for ignore this argument.
*/
void removeItem(IWorldItem* item,
QList<int>* removedObjectsList = nullptr);
/**
* @brief addAtomicItem This method execure atomic operation of add new item. This method support only atomic objects. (not clasters)
* @param obj This is pointer to the atomic object. If the object are claster then it will be added without childs objects.