ActiveGrenade: Fixed a potential leak by checking entities smokegrens life if RemoveGrenade call is somehow missing or blocked

This commit is contained in:
s1lentq 2024-05-09 00:20:22 +07:00
parent 8ff1bf1232
commit cabdc254c7
3 changed files with 24 additions and 6 deletions

View File

@ -491,9 +491,15 @@ void ActiveGrenade::OnEntityGone()
m_entity = nullptr; m_entity = nullptr;
} }
void ActiveGrenade::CheckOnEntityGone()
{
if (m_dieTimestamp == 0 && !m_entity.IsValid())
OnEntityGone();
}
bool ActiveGrenade::IsValid() const bool ActiveGrenade::IsValid() const
{ {
if (!m_entity) if (!m_entity.IsValid())
{ {
if (gpGlobals->time > m_dieTimestamp) if (gpGlobals->time > m_dieTimestamp)
return false; return false;
@ -502,7 +508,7 @@ bool ActiveGrenade::IsValid() const
return true; return true;
} }
const Vector *ActiveGrenade::GetPosition() const const Vector *ActiveGrenade::GetPosition()
{ {
return &m_entity->pev->origin; return &m_entity->pev->origin;
} }

View File

@ -147,6 +147,8 @@ void CBotManager::StartFrame()
ActiveGrenade *ag = (*iter); ActiveGrenade *ag = (*iter);
// lazy validation // lazy validation
ag->CheckOnEntityGone();
if (!ag->IsValid()) if (!ag->IsValid())
{ {
delete ag; delete ag;
@ -203,6 +205,8 @@ void CBotManager::StartFrame()
pBot->BotThink(); pBot->BotThink();
} }
} }
ValidateActiveGrenades();
} }
// Return the filename for this map's "nav map" file // Return the filename for this map's "nav map" file
@ -278,13 +282,16 @@ void CBotManager::RemoveGrenade(CGrenade *grenade)
} }
// Destroy any invalid active grenades // Destroy any invalid active grenades
NOXREF void CBotManager::ValidateActiveGrenades() void CBotManager::ValidateActiveGrenades()
{ {
auto iter = m_activeGrenadeList.begin(); auto iter = m_activeGrenadeList.begin();
while (iter != m_activeGrenadeList.end()) while (iter != m_activeGrenadeList.end())
{ {
ActiveGrenade *ag = (*iter); ActiveGrenade *ag = (*iter);
// lazy validation
ag->CheckOnEntityGone();
if (!ag->IsValid()) if (!ag->IsValid())
{ {
delete ag; delete ag;
@ -314,6 +321,8 @@ bool CBotManager::IsInsideSmokeCloud(const Vector *pos)
ActiveGrenade *ag = (*iter); ActiveGrenade *ag = (*iter);
// lazy validation // lazy validation
ag->CheckOnEntityGone();
if (!ag->IsValid()) if (!ag->IsValid())
{ {
delete ag; delete ag;
@ -358,6 +367,8 @@ bool CBotManager::IsLineBlockedBySmoke(const Vector *from, const Vector *to)
ActiveGrenade *ag = (*iter); ActiveGrenade *ag = (*iter);
// lazy validation // lazy validation
ag->CheckOnEntityGone();
if (!ag->IsValid()) if (!ag->IsValid())
{ {
delete ag; delete ag;

View File

@ -42,16 +42,17 @@ public:
ActiveGrenade(int weaponID, CGrenade *grenadeEntity); ActiveGrenade(int weaponID, CGrenade *grenadeEntity);
void OnEntityGone(); void OnEntityGone();
void CheckOnEntityGone();
bool IsValid() const; bool IsValid() const;
bool IsEntity(CGrenade *grenade) const { return (grenade == m_entity) ? true : false; } bool IsEntity(CGrenade *grenade) const { return grenade == m_entity; }
int GetID() const { return m_id; } int GetID() const { return m_id; }
const Vector *GetDetonationPosition() const { return &m_detonationPosition; } const Vector *GetDetonationPosition() const { return &m_detonationPosition; }
const Vector *GetPosition() const; const Vector *GetPosition();
private: private:
int m_id; int m_id;
CGrenade *m_entity; EntityHandle<CGrenade> m_entity;
Vector m_detonationPosition; Vector m_detonationPosition;
float m_dieTimestamp; float m_dieTimestamp;
}; };