fix(Core/Movement): fix a small memory leak in RandomMovementGenerator (#20258)

* fix(Core/Movement): memory leak and possible crash

Co-Authored-By: Ovahlord <18347559+ovahlord@users.noreply.github.com>

* .

* fix review

---------

Co-authored-by: Ovahlord <18347559+ovahlord@users.noreply.github.com>
This commit is contained in:
Grimdhex
2024-11-01 13:39:37 +01:00
committed by GitHub
parent 4dcaee1816
commit 0b031de2ea
2 changed files with 9 additions and 7 deletions

View File

@@ -29,15 +29,14 @@
template<class T>
RandomMovementGenerator<T>::~RandomMovementGenerator() { }
template<>
RandomMovementGenerator<Creature>::~RandomMovementGenerator()
{
delete _pathGenerator;
}
template RandomMovementGenerator<Creature>::~RandomMovementGenerator();
template<>
void RandomMovementGenerator<Creature>::_setRandomLocation(Creature* creature)
{
if (!creature)
return;
if (creature->_moveState != MAP_OBJECT_CELL_MOVE_NONE)
return;
@@ -135,7 +134,7 @@ void RandomMovementGenerator<Creature>::_setRandomLocation(Creature* creature)
else // ground
{
if (!_pathGenerator)
_pathGenerator = new PathGenerator(creature);
_pathGenerator = std::make_unique<PathGenerator>(creature);
else
_pathGenerator->Clear();
@@ -242,6 +241,8 @@ void RandomMovementGenerator<Creature>::DoInitialize(Creature* creature)
if (!_wanderDistance)
_wanderDistance = creature->GetWanderDistance();
_pathGenerator.reset();
_nextMoveTime.Reset(creature->GetSpawnId() && creature->GetWanderDistance() == _wanderDistance ? urand(1, 5000) : 0);
_wanderDistance = std::max((creature->GetWanderDistance() == _wanderDistance && creature->GetInstanceId() == 0) ? (creature->CanFly() ? MIN_WANDER_DISTANCE_AIR : MIN_WANDER_DISTANCE_GROUND) : 0.0f, _wanderDistance);
@@ -280,6 +281,7 @@ bool RandomMovementGenerator<Creature>::DoUpdate(Creature* creature, const uint3
{
_nextMoveTime.Reset(0); // Expire the timer
creature->StopMoving();
_pathGenerator.reset();
return true;
}

View File

@@ -61,7 +61,7 @@ private:
TimeTrackerSmall _nextMoveTime;
uint8 _moveCount;
float _wanderDistance;
PathGenerator* _pathGenerator;
std::unique_ptr<PathGenerator> _pathGenerator;
std::vector<G3D::Vector3> _destinationPoints;
std::vector<uint8> _validPointsVector[RANDOM_POINTS_NUMBER + 1];
uint8 _currentPoint;