Make clearBuffs memory-safe

improperly erasing during iteration oops
This commit is contained in:
gsemaj 2023-08-13 18:26:30 -07:00
parent 478ae3d996
commit da33c5c30f
No known key found for this signature in database
GPG Key ID: 24B96BAA40497929

View File

@ -61,15 +61,14 @@ void Player::removeBuff(int buffId, BuffClass buffClass) {
} }
void Player::clearBuffs(bool force) { void Player::clearBuffs(bool force) {
for(auto buff : buffs) { auto it = buffs.begin();
if(!force) { while(it != buffs.end()) {
removeBuff(buff.first); Buff* buff = (*it).second;
} else { if(!force) buff->clear();
delete buff.second; delete buff;
it = buffs.erase(it);
} }
} }
buffs.clear();
}
bool Player::hasBuff(int buffId) { bool Player::hasBuff(int buffId) {
auto buff = buffs.find(buffId); auto buff = buffs.find(buffId);
@ -217,15 +216,14 @@ void CombatNPC::removeBuff(int buffId, BuffClass buffClass) {
} }
void CombatNPC::clearBuffs(bool force) { void CombatNPC::clearBuffs(bool force) {
for(auto buff : buffs) { auto it = buffs.begin();
if(!force) { while(it != buffs.end()) {
removeBuff(buff.first); Buff* buff = (*it).second;
} else { if(!force) buff->clear();
delete buff.second; delete buff;
it = buffs.erase(it);
} }
} }
buffs.clear();
}
bool CombatNPC::hasBuff(int buffId) { bool CombatNPC::hasBuff(int buffId) {
auto buff = buffs.find(buffId); auto buff = buffs.find(buffId);