Skip to content

Commit

Permalink
ActiveGrenade: Fixed a potential leak by checking entities smokegrens…
Browse files Browse the repository at this point in the history
… life if RemoveGrenade call is somehow missing or blocked
  • Loading branch information
s1lentq committed May 8, 2024
1 parent 8ff1bf1 commit cabdc25
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
10 changes: 8 additions & 2 deletions regamedll/game_shared/bot/bot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -491,9 +491,15 @@ void ActiveGrenade::OnEntityGone()
m_entity = nullptr;
}

void ActiveGrenade::CheckOnEntityGone()
{
if (m_dieTimestamp == 0 && !m_entity.IsValid())
OnEntityGone();
}

bool ActiveGrenade::IsValid() const
{
if (!m_entity)
if (!m_entity.IsValid())
{
if (gpGlobals->time > m_dieTimestamp)
return false;
Expand All @@ -502,7 +508,7 @@ bool ActiveGrenade::IsValid() const
return true;
}

const Vector *ActiveGrenade::GetPosition() const
const Vector *ActiveGrenade::GetPosition()
{
return &m_entity->pev->origin;
}
13 changes: 12 additions & 1 deletion regamedll/game_shared/bot/bot_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ void CBotManager::StartFrame()
ActiveGrenade *ag = (*iter);

// lazy validation
ag->CheckOnEntityGone();

if (!ag->IsValid())
{
delete ag;
Expand Down Expand Up @@ -203,6 +205,8 @@ void CBotManager::StartFrame()
pBot->BotThink();
}
}

ValidateActiveGrenades();
}

// Return the filename for this map's "nav map" file
Expand Down Expand Up @@ -278,13 +282,16 @@ void CBotManager::RemoveGrenade(CGrenade *grenade)
}

// Destroy any invalid active grenades
NOXREF void CBotManager::ValidateActiveGrenades()
void CBotManager::ValidateActiveGrenades()
{
auto iter = m_activeGrenadeList.begin();
while (iter != m_activeGrenadeList.end())
{
ActiveGrenade *ag = (*iter);

// lazy validation
ag->CheckOnEntityGone();

if (!ag->IsValid())
{
delete ag;
Expand Down Expand Up @@ -314,6 +321,8 @@ bool CBotManager::IsInsideSmokeCloud(const Vector *pos)
ActiveGrenade *ag = (*iter);

// lazy validation
ag->CheckOnEntityGone();

if (!ag->IsValid())
{
delete ag;
Expand Down Expand Up @@ -358,6 +367,8 @@ bool CBotManager::IsLineBlockedBySmoke(const Vector *from, const Vector *to)
ActiveGrenade *ag = (*iter);

// lazy validation
ag->CheckOnEntityGone();

if (!ag->IsValid())
{
delete ag;
Expand Down
7 changes: 4 additions & 3 deletions regamedll/game_shared/bot/bot_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,17 @@ class ActiveGrenade
ActiveGrenade(int weaponID, CGrenade *grenadeEntity);

void OnEntityGone();
void CheckOnEntityGone();
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; }
const Vector *GetDetonationPosition() const { return &m_detonationPosition; }
const Vector *GetPosition() const;
const Vector *GetPosition();

private:
int m_id;
CGrenade *m_entity;
EntityHandle<CGrenade> m_entity;
Vector m_detonationPosition;
float m_dieTimestamp;
};
Expand Down

0 comments on commit cabdc25

Please sign in to comment.