Skip to content

Commit cabdc25

Browse files
committed
ActiveGrenade: Fixed a potential leak by checking entities smokegrens life if RemoveGrenade call is somehow missing or blocked
1 parent 8ff1bf1 commit cabdc25

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

regamedll/game_shared/bot/bot.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -491,9 +491,15 @@ void ActiveGrenade::OnEntityGone()
491491
m_entity = nullptr;
492492
}
493493

494+
void ActiveGrenade::CheckOnEntityGone()
495+
{
496+
if (m_dieTimestamp == 0 && !m_entity.IsValid())
497+
OnEntityGone();
498+
}
499+
494500
bool ActiveGrenade::IsValid() const
495501
{
496-
if (!m_entity)
502+
if (!m_entity.IsValid())
497503
{
498504
if (gpGlobals->time > m_dieTimestamp)
499505
return false;
@@ -502,7 +508,7 @@ bool ActiveGrenade::IsValid() const
502508
return true;
503509
}
504510

505-
const Vector *ActiveGrenade::GetPosition() const
511+
const Vector *ActiveGrenade::GetPosition()
506512
{
507513
return &m_entity->pev->origin;
508514
}

regamedll/game_shared/bot/bot_manager.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@ void CBotManager::StartFrame()
147147
ActiveGrenade *ag = (*iter);
148148

149149
// lazy validation
150+
ag->CheckOnEntityGone();
151+
150152
if (!ag->IsValid())
151153
{
152154
delete ag;
@@ -203,6 +205,8 @@ void CBotManager::StartFrame()
203205
pBot->BotThink();
204206
}
205207
}
208+
209+
ValidateActiveGrenades();
206210
}
207211

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

280284
// Destroy any invalid active grenades
281-
NOXREF void CBotManager::ValidateActiveGrenades()
285+
void CBotManager::ValidateActiveGrenades()
282286
{
283287
auto iter = m_activeGrenadeList.begin();
284288
while (iter != m_activeGrenadeList.end())
285289
{
286290
ActiveGrenade *ag = (*iter);
287291

292+
// lazy validation
293+
ag->CheckOnEntityGone();
294+
288295
if (!ag->IsValid())
289296
{
290297
delete ag;
@@ -314,6 +321,8 @@ bool CBotManager::IsInsideSmokeCloud(const Vector *pos)
314321
ActiveGrenade *ag = (*iter);
315322

316323
// lazy validation
324+
ag->CheckOnEntityGone();
325+
317326
if (!ag->IsValid())
318327
{
319328
delete ag;
@@ -358,6 +367,8 @@ bool CBotManager::IsLineBlockedBySmoke(const Vector *from, const Vector *to)
358367
ActiveGrenade *ag = (*iter);
359368

360369
// lazy validation
370+
ag->CheckOnEntityGone();
371+
361372
if (!ag->IsValid())
362373
{
363374
delete ag;

regamedll/game_shared/bot/bot_manager.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,17 @@ class ActiveGrenade
4242
ActiveGrenade(int weaponID, CGrenade *grenadeEntity);
4343

4444
void OnEntityGone();
45+
void CheckOnEntityGone();
4546
bool IsValid() const;
4647

47-
bool IsEntity(CGrenade *grenade) const { return (grenade == m_entity) ? true : false; }
48+
bool IsEntity(CGrenade *grenade) const { return grenade == m_entity; }
4849
int GetID() const { return m_id; }
4950
const Vector *GetDetonationPosition() const { return &m_detonationPosition; }
50-
const Vector *GetPosition() const;
51+
const Vector *GetPosition();
5152

5253
private:
5354
int m_id;
54-
CGrenade *m_entity;
55+
EntityHandle<CGrenade> m_entity;
5556
Vector m_detonationPosition;
5657
float m_dieTimestamp;
5758
};

0 commit comments

Comments
 (0)