Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pathfinder memory leak fix and actually use saved polyRefs. #502

Closed
wants to merge 2 commits into from

Conversation

mostlikely4r
Copy link
Contributor

@mostlikely4r mostlikely4r commented May 31, 2023

🍰 Pullrequest

This pull request fixes the pathfinders ability to actually use previously saved polyRefs and stops the same from being saved over and over.

An issue was found by looking for memory leaks. For a server that was running for multiple hours Pathfinder.m_pathPolyRefs

std::vector<dtPolyRef> m_pathPolyRefs; // array of detour polygon references

was filled with many duplicate records. Futher inspection showed these duplicate records are not used.

This issue is introduced in (9c0a31b) and probably the result of an imcomplete change.

This PR fixes the issue where saved polyRefs are not properly found for later use. This also attempts to prevent the same polyRef from being saved over and over again.

Proof

When looking for polys the pathfinder will first look at polys saved in the current path:

dtPolyRef polyRef = getPathPolyByPosition(m_pathPolyRefs.data(), m_polyLength, point, distance);

However if no suitable poly is found two broader searches are executed to find a result:
dtStatus dtResult = m_navMeshQuery->findNearestPoly(point, NearPolySearchBound, &m_filter, &polyRef, closestPoint);

Any found poly is then saved in Pathfinder.m_pathPolyRefs with a pushback in:
m_pathPolyRefs.push_back(polyRef);

Pathfinder.m_pathPolyRefs however used to be an array but is now a vector that is initalized with a default size of MAX_POINT_PATH_LENGTH in:

m_cachedPoints(m_pointPathLimit * VERTEX_SIZE), m_pathPolyRefs(m_pointPathLimit), m_polyLength(0),

This means that new polyRefs pushed_back into Pathfinder.m_pathPolyRefs will be at the end at location MAX_POINT_PATH_LENGTH+(number of saved polyPaths).
However when looping over this vector it loops from 0 to m_polyLength in:
for (uint32 i = 0; i < polyPathSize; ++i)

Since m_polyLength is the length of the path the extra Polys at the end of the vector are never found.

The result is in:

dtPolyRef PathFinder::getPolyByLocation(const float* point, float* distance)

-The path is checked for a suitable poly.
-If none is found a broader search is done using the navmesh query.
-Any result is saved at the end of the vector containing the path.
-The next search will still not find this previously saved poly in the path part of the vector.
-Again a search is done with navmesh query.
-The result is saved again in the vector.
-Ect.

Issues

This fixes a memory leak for units that do pathfinding over a longer time.
This 'should' give some performance increase because it prevents some extra navmesh queries.

How2Test

While the server is running for a little while, put a breakpoint at:

dtPolyRef PathFinder::getPolyByLocation(const float* point, float* distance)

Notice that this->m_pathPolyRefs does not contain many duplicate records at the end of the vector and the size isn't too many times MAX_POINT_PATH_LENGTH.

Todo / Checklist

An alternative to this PR is to simply remove the push_backs until futher notice or refactor the pathfinding code to initialize m_pathPolyRefs without a size and dynamically size the vector when needed instead. Ideally I'd like to discuss proper implementation of 9c0a31b with the autor or anyone who has a decent graps of the pathfinder code.

@mostlikely4r
Copy link
Contributor Author

I see I added a merge commit to this PR. I'll remake the PR.

killerwife pushed a commit that referenced this pull request Mar 24, 2024
* Config: Implement config override with env vars

Implement overriding of configuration from the .conf file with environment variables.
Environment variables keys are autogenerated based on the keys defined in .conf file.
Usage example:
$ export CM_DATA_DIR=/usr
$ CM_WORLD_SERVER_PORT=8080 ./mangosd

* Update env var key format and encapsulated env loading logic.

The new env key format:
Mangosd_Rate_Health
Mangosd_DataDir

* Add suggestions from code review.

* Add missing includes.
davidonete pushed a commit to davidonete/mangos-classic that referenced this pull request Mar 25, 2024
* Config: Implement config override with env vars

Implement overriding of configuration from the .conf file with environment variables.
Environment variables keys are autogenerated based on the keys defined in .conf file.
Usage example:
$ export CM_DATA_DIR=/usr
$ CM_WORLD_SERVER_PORT=8080 ./mangosd

* Update env var key format and encapsulated env loading logic.

The new env key format:
Mangosd_Rate_Health
Mangosd_DataDir

* Add suggestions from code review.

* Add missing includes.
davidonete added a commit to davidonete/mangos-classic that referenced this pull request Mar 26, 2024
* Remove gameobject los temp fix for playerbots

* Chat: Fix npc info parts not working with dynguid

* StringId: Add const qualifiers to params to enable passing const strings

* SpawnGroup: Add errors for using same type dbGuid twice

* Spell/Unit: Disable charge proccing in proc system for spellmods

Closes cmangos/issues#3648

* Network: Remove built in nagle algo and fully go async (#653)

* Network: Remove built in nagle algo and fully go async

* Fix compilation due to printf error

* Fix hang on shutdown

* Add changes requested in comments

* Make last session key update in sequence sync to avoid race condition with db

* Realm/Core: Improve start script for mangosd and add realmd one

* Realmd: Remove leftover printf

* BRD: Adjust motion type used by Nagmara & Rocknot

Mistress Nagmara 9500 (npc_mistress_nagmara)
Private Rocknot 9503 (npc_rocknot)

cmangos/issues#3529

* Realmd/Core: Adjust username length to proper max length allowed during creation

* Fix compilation errors

* Spell/Engineering: Goblin Bomb Dispenser

* Use item 10587 "Goblin Bomb Dispenser" - casts spell 23134 "Goblin Bomb"
* 10% chance to malfunction with spell 13261 "Malfunction Explosion or 90% chance to proceed with spell 13258 "Summon Goblin Bomb"
* Creature 8937 "Pet Bomb" is summoned - level is player's engineering skill divided by 5
* Pet Bomb has aura 13260 "Pet Bomb Passive" on spawn - triggers 13259 "Explosion" on successful melee hit

cmangos/issues#637

* Spell: Reimplement SPELL_ATTR_SCALES_WITH_CREATURE_LEVEL using CLS as confirmed

Original vmangos authors:

@balakethelock
@ratkosrb

* Spell: Safeguard go use which has lockId and should be used by spell

* Add couple safeguards to ProcessIncomingData against crashes

cmangos/issues#3672

* Item: Add check against RequiredLevel for lootable items

* Gameobject: Port GetSpellForLock from tbc

* [z2819] Item: Adjust itemTextId in item_instance to correspond to packet, load and item_text width

* Creature: Implement CreatureLinkingAggroDelay

* Network: Add missing error return clause to WorldSocket::ProcessIncomingData

* Network: Do not disconnect session when packet cooldown is triggered

* Creature: Fix crash due to missing enemy safeguard

* AI: Disable assistance call for critters

* Scripts: Fix SCRIPT_COMMAND_OPEN_DOOR not working with dynguid

* Chat: Implement .worldstate set X Y

* PeriodicTriggerData: fix member initializer list.

This also fixes a GCC/clang 'unused parameter' [-Wunused-parameter]
compiler warning.

* Fix memory leak, leaking a single script.

* Network: Add a couple missing captures

* Scripts: Implement SCRIPT_FLAG_COMMAND_ADDITIONAL for SCRIPT_COMMAND_CAST_SPELL to pass nullptr target

* Network: Rework reconnection case to fix slow cpu race conditions
cmangos/issues#3676

* Adjust CreatureFamilyAssistanceRadius being too big

* [z2820] Auth: Adjust joindate to be datetime

* Network: Add back error handling from old code for packet header in mangosd

* Tools/MMaps: Fixed a suspicious bitwise condition.

ProjectSkyfire/SkyFire_548@89063ad

* Creature: Reimplement Creature::LockOutSpells with CreatureStaticFlags2::NO_INTERRUPT_SCHOOL_COOLDOWN

* Player: Add afk kick after 15 minutes back in

* Network: Remove redundant payload of SMSG_AUTH_CHALLENGE

Wotlk +

@ratkosrb authorship for noticing

* Spell: Add flag usage to SMSG_SPELL_COOLDOWN instead of random value

* Relations: Implement caster and target flag skip from attributes

* Spell: Implement usage of restriction skip attributes for basic things
Also ports swiftmend check to spell script to remove merge conflict

* fix ProcSystemArguments constructor arg mismatch

* [Quest] rework Missing in Action quest using new systems (#660)

* [Quest] rework Missing in Action quest using new systems

* add changes requested in comments

* SpawnGroup: Implement CREATURE_GROUP_FORMATION_MIRRORING

* Config: Implement config override with env vars (cmangos#502)

* Config: Implement config override with env vars

Implement overriding of configuration from the .conf file with environment variables.
Environment variables keys are autogenerated based on the keys defined in .conf file.
Usage example:
$ export CM_DATA_DIR=/usr
$ CM_WORLD_SERVER_PORT=8080 ./mangosd

* Update env var key format and encapsulated env loading logic.

The new env key format:
Mangosd_Rate_Health
Mangosd_DataDir

* Add suggestions from code review.

* Add missing includes.

* Pet: Tryfix double unsummon crash due to visibility

Close cmangos/mangos-wotlk#508

Credit @insunaa for the general idea

* [Quest] modernize defias brotherhood escort quest
Closes cmangos/mangos-tbc#662

* [Quest] modernize 'Protect the Shipment' escort quest
Closes cmangos/mangos-tbc#663

* G3D: Extend Support for new MacOS Versions (+Apple Silicon)

* Pet: Fix compilation for pch on linux

* G3D: Fix MacOS/Apple Silicon compile

* Playerbots: Fix compilation issue

---------

Co-authored-by: killerwife <[email protected]>
Co-authored-by: MantisLord <[email protected]>
Co-authored-by: Fonix <[email protected]>
Co-authored-by: robinsch <[email protected]>
Co-authored-by: evil-at-wow <[email protected]>
Co-authored-by: cdkr <[email protected]>
Co-authored-by: _daanieL <[email protected]>
Co-authored-by: Anton Popovichenko <[email protected]>
Co-authored-by: insunaa <[email protected]>
davidonete added a commit to davidonete/mangos-classic that referenced this pull request Apr 3, 2024
* Remove gameobject los temp fix for playerbots

* Chat: Fix npc info parts not working with dynguid

* StringId: Add const qualifiers to params to enable passing const strings

* SpawnGroup: Add errors for using same type dbGuid twice

* Spell/Unit: Disable charge proccing in proc system for spellmods

Closes cmangos/issues#3648

* Network: Remove built in nagle algo and fully go async (#653)

* Network: Remove built in nagle algo and fully go async

* Fix compilation due to printf error

* Fix hang on shutdown

* Add changes requested in comments

* Make last session key update in sequence sync to avoid race condition with db

* Realm/Core: Improve start script for mangosd and add realmd one

* Realmd: Remove leftover printf

* BRD: Adjust motion type used by Nagmara & Rocknot

Mistress Nagmara 9500 (npc_mistress_nagmara)
Private Rocknot 9503 (npc_rocknot)

cmangos/issues#3529

* Realmd/Core: Adjust username length to proper max length allowed during creation

* Fix compilation errors

* Spell/Engineering: Goblin Bomb Dispenser

* Use item 10587 "Goblin Bomb Dispenser" - casts spell 23134 "Goblin Bomb"
* 10% chance to malfunction with spell 13261 "Malfunction Explosion or 90% chance to proceed with spell 13258 "Summon Goblin Bomb"
* Creature 8937 "Pet Bomb" is summoned - level is player's engineering skill divided by 5
* Pet Bomb has aura 13260 "Pet Bomb Passive" on spawn - triggers 13259 "Explosion" on successful melee hit

cmangos/issues#637

* Spell: Reimplement SPELL_ATTR_SCALES_WITH_CREATURE_LEVEL using CLS as confirmed

Original vmangos authors:

@balakethelock
@ratkosrb

* Spell: Safeguard go use which has lockId and should be used by spell

* Add couple safeguards to ProcessIncomingData against crashes

cmangos/issues#3672

* Item: Add check against RequiredLevel for lootable items

* Gameobject: Port GetSpellForLock from tbc

* [z2819] Item: Adjust itemTextId in item_instance to correspond to packet, load and item_text width

* Creature: Implement CreatureLinkingAggroDelay

* Network: Add missing error return clause to WorldSocket::ProcessIncomingData

* Network: Do not disconnect session when packet cooldown is triggered

* Creature: Fix crash due to missing enemy safeguard

* AI: Disable assistance call for critters

* Scripts: Fix SCRIPT_COMMAND_OPEN_DOOR not working with dynguid

* Chat: Implement .worldstate set X Y

* PeriodicTriggerData: fix member initializer list.

This also fixes a GCC/clang 'unused parameter' [-Wunused-parameter]
compiler warning.

* Fix memory leak, leaking a single script.

* Network: Add a couple missing captures

* Scripts: Implement SCRIPT_FLAG_COMMAND_ADDITIONAL for SCRIPT_COMMAND_CAST_SPELL to pass nullptr target

* Network: Rework reconnection case to fix slow cpu race conditions
cmangos/issues#3676

* Adjust CreatureFamilyAssistanceRadius being too big

* [z2820] Auth: Adjust joindate to be datetime

* Network: Add back error handling from old code for packet header in mangosd

* Tools/MMaps: Fixed a suspicious bitwise condition.

ProjectSkyfire/SkyFire_548@89063ad

* Creature: Reimplement Creature::LockOutSpells with CreatureStaticFlags2::NO_INTERRUPT_SCHOOL_COOLDOWN

* Player: Add afk kick after 15 minutes back in

* Network: Remove redundant payload of SMSG_AUTH_CHALLENGE

Wotlk +

@ratkosrb authorship for noticing

* Spell: Add flag usage to SMSG_SPELL_COOLDOWN instead of random value

* Relations: Implement caster and target flag skip from attributes

* Spell: Implement usage of restriction skip attributes for basic things
Also ports swiftmend check to spell script to remove merge conflict

* fix ProcSystemArguments constructor arg mismatch

* [Quest] rework Missing in Action quest using new systems (#660)

* [Quest] rework Missing in Action quest using new systems

* add changes requested in comments

* SpawnGroup: Implement CREATURE_GROUP_FORMATION_MIRRORING

* Config: Implement config override with env vars (cmangos#502)

* Config: Implement config override with env vars

Implement overriding of configuration from the .conf file with environment variables.
Environment variables keys are autogenerated based on the keys defined in .conf file.
Usage example:
$ export CM_DATA_DIR=/usr
$ CM_WORLD_SERVER_PORT=8080 ./mangosd

* Update env var key format and encapsulated env loading logic.

The new env key format:
Mangosd_Rate_Health
Mangosd_DataDir

* Add suggestions from code review.

* Add missing includes.

* Pet: Tryfix double unsummon crash due to visibility

Close cmangos/mangos-wotlk#508

Credit @insunaa for the general idea

* [Quest] modernize defias brotherhood escort quest
Closes cmangos/mangos-tbc#662

* [Quest] modernize 'Protect the Shipment' escort quest
Closes cmangos/mangos-tbc#663

* G3D: Extend Support for new MacOS Versions (+Apple Silicon)

* Pet: Fix compilation for pch on linux

* G3D: Fix MacOS/Apple Silicon compile

* Playerbots: Fix compilation issue

* Playerbots: Fix grid loading issue (cmangos#536)

* Core/PacketIO: Correct and add field names for SMSG_PETITION_QUERY_RESPONSE

Picked from: https://github.com/vmangos/core

Co-authored-by: brotalnia <[email protected]>
Co-authored-by: GrenderG <[email protected]>

vmangos/core@b9b006d
vmangos/core@e349565

* Magmadar panic should have a higher cooldown

---------

Co-authored-by: killerwife <[email protected]>
Co-authored-by: MantisLord <[email protected]>
Co-authored-by: Fonix <[email protected]>
Co-authored-by: robinsch <[email protected]>
Co-authored-by: evil-at-wow <[email protected]>
Co-authored-by: cdkr <[email protected]>
Co-authored-by: _daanieL <[email protected]>
Co-authored-by: Anton Popovichenko <[email protected]>
Co-authored-by: insunaa <[email protected]>
Co-authored-by: Andreas Titan <[email protected]>
davidonete added a commit to davidonete/mangos-classic that referenced this pull request Apr 4, 2024
* Remove gameobject los temp fix for playerbots

* Chat: Fix npc info parts not working with dynguid

* StringId: Add const qualifiers to params to enable passing const strings

* SpawnGroup: Add errors for using same type dbGuid twice

* Spell/Unit: Disable charge proccing in proc system for spellmods

Closes cmangos/issues#3648

* Network: Remove built in nagle algo and fully go async (#653)

* Network: Remove built in nagle algo and fully go async

* Fix compilation due to printf error

* Fix hang on shutdown

* Add changes requested in comments

* Make last session key update in sequence sync to avoid race condition with db

* Realm/Core: Improve start script for mangosd and add realmd one

* Realmd: Remove leftover printf

* BRD: Adjust motion type used by Nagmara & Rocknot

Mistress Nagmara 9500 (npc_mistress_nagmara)
Private Rocknot 9503 (npc_rocknot)

cmangos/issues#3529

* Realmd/Core: Adjust username length to proper max length allowed during creation

* Fix compilation errors

* Spell/Engineering: Goblin Bomb Dispenser

* Use item 10587 "Goblin Bomb Dispenser" - casts spell 23134 "Goblin Bomb"
* 10% chance to malfunction with spell 13261 "Malfunction Explosion or 90% chance to proceed with spell 13258 "Summon Goblin Bomb"
* Creature 8937 "Pet Bomb" is summoned - level is player's engineering skill divided by 5
* Pet Bomb has aura 13260 "Pet Bomb Passive" on spawn - triggers 13259 "Explosion" on successful melee hit

cmangos/issues#637

* Spell: Reimplement SPELL_ATTR_SCALES_WITH_CREATURE_LEVEL using CLS as confirmed

Original vmangos authors:

@balakethelock
@ratkosrb

* Spell: Safeguard go use which has lockId and should be used by spell

* Add couple safeguards to ProcessIncomingData against crashes

cmangos/issues#3672

* Item: Add check against RequiredLevel for lootable items

* Gameobject: Port GetSpellForLock from tbc

* [z2819] Item: Adjust itemTextId in item_instance to correspond to packet, load and item_text width

* Creature: Implement CreatureLinkingAggroDelay

* Network: Add missing error return clause to WorldSocket::ProcessIncomingData

* Network: Do not disconnect session when packet cooldown is triggered

* Creature: Fix crash due to missing enemy safeguard

* AI: Disable assistance call for critters

* Scripts: Fix SCRIPT_COMMAND_OPEN_DOOR not working with dynguid

* Chat: Implement .worldstate set X Y

* PeriodicTriggerData: fix member initializer list.

This also fixes a GCC/clang 'unused parameter' [-Wunused-parameter]
compiler warning.

* Fix memory leak, leaking a single script.

* Network: Add a couple missing captures

* Scripts: Implement SCRIPT_FLAG_COMMAND_ADDITIONAL for SCRIPT_COMMAND_CAST_SPELL to pass nullptr target

* Network: Rework reconnection case to fix slow cpu race conditions
cmangos/issues#3676

* Adjust CreatureFamilyAssistanceRadius being too big

* [z2820] Auth: Adjust joindate to be datetime

* Network: Add back error handling from old code for packet header in mangosd

* Tools/MMaps: Fixed a suspicious bitwise condition.

ProjectSkyfire/SkyFire_548@89063ad

* Creature: Reimplement Creature::LockOutSpells with CreatureStaticFlags2::NO_INTERRUPT_SCHOOL_COOLDOWN

* Player: Add afk kick after 15 minutes back in

* Network: Remove redundant payload of SMSG_AUTH_CHALLENGE

Wotlk +

@ratkosrb authorship for noticing

* Spell: Add flag usage to SMSG_SPELL_COOLDOWN instead of random value

* Relations: Implement caster and target flag skip from attributes

* Spell: Implement usage of restriction skip attributes for basic things
Also ports swiftmend check to spell script to remove merge conflict

* fix ProcSystemArguments constructor arg mismatch

* [Quest] rework Missing in Action quest using new systems (#660)

* [Quest] rework Missing in Action quest using new systems

* add changes requested in comments

* SpawnGroup: Implement CREATURE_GROUP_FORMATION_MIRRORING

* Config: Implement config override with env vars (cmangos#502)

* Config: Implement config override with env vars

Implement overriding of configuration from the .conf file with environment variables.
Environment variables keys are autogenerated based on the keys defined in .conf file.
Usage example:
$ export CM_DATA_DIR=/usr
$ CM_WORLD_SERVER_PORT=8080 ./mangosd

* Update env var key format and encapsulated env loading logic.

The new env key format:
Mangosd_Rate_Health
Mangosd_DataDir

* Add suggestions from code review.

* Add missing includes.

* Pet: Tryfix double unsummon crash due to visibility

Close cmangos/mangos-wotlk#508

Credit @insunaa for the general idea

* [Quest] modernize defias brotherhood escort quest
Closes cmangos/mangos-tbc#662

* [Quest] modernize 'Protect the Shipment' escort quest
Closes cmangos/mangos-tbc#663

* G3D: Extend Support for new MacOS Versions (+Apple Silicon)

* Pet: Fix compilation for pch on linux

* G3D: Fix MacOS/Apple Silicon compile

* Playerbots: Fix compilation issue

* Playerbots: Fix grid loading issue (cmangos#536)

* Core/PacketIO: Correct and add field names for SMSG_PETITION_QUERY_RESPONSE

Picked from: https://github.com/vmangos/core

Co-authored-by: brotalnia <[email protected]>
Co-authored-by: GrenderG <[email protected]>

vmangos/core@b9b006d
vmangos/core@e349565

* Magmadar panic should have a higher cooldown

* Playerbots: Handle spell item requirements by the bot system

---------

Co-authored-by: killerwife <[email protected]>
Co-authored-by: MantisLord <[email protected]>
Co-authored-by: Fonix <[email protected]>
Co-authored-by: robinsch <[email protected]>
Co-authored-by: evil-at-wow <[email protected]>
Co-authored-by: cdkr <[email protected]>
Co-authored-by: _daanieL <[email protected]>
Co-authored-by: Anton Popovichenko <[email protected]>
Co-authored-by: insunaa <[email protected]>
Co-authored-by: Andreas Titan <[email protected]>
@mostlikely4r mostlikely4r deleted the patch-1 branch July 5, 2024 12:09
@mostlikely4r mostlikely4r restored the patch-1 branch July 5, 2024 12:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant