Skip to content

Commit

Permalink
fix: remove target without accessing freed memory (opentibiabr#786)
Browse files Browse the repository at this point in the history
This change addresses an issue where the function removeTarget was accessing a pointer after it had been freed by decrementing its reference counter. This was causing a use of memory after it was freed error. This ensures that the pointer is not accessed after it has been freed. Also moves the decrement reference counter line to the end of the function, making sure that the pointer is not accessed after it is freed.
  • Loading branch information
dudantas committed Jan 17, 2023
1 parent 42a524b commit 2f252e2
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/creatures/monsters/monster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -355,12 +355,12 @@ void Monster::removeTarget(Creature* creature)

auto it = std::find(targetList.begin(), targetList.end(), creature);
if (it != targetList.end()) {
creature->decrementReferenceCounter();
targetList.erase(it);

if (!master && getFaction() != FACTION_DEFAULT && creature->getPlayer()) {
totalPlayersOnScreen--;
}

creature->decrementReferenceCounter();
targetList.erase(it);
}
}

Expand Down

0 comments on commit 2f252e2

Please sign in to comment.