Skip to content

Commit

Permalink
try fix a crash due to the deletion of an item in an unusual way
Browse files Browse the repository at this point in the history
related #870, #752, #924, #957
  • Loading branch information
s1lentq committed Aug 6, 2024
1 parent 279799b commit 77a3f2b
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions regamedll/dlls/cbase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1598,6 +1598,30 @@ void OnFreeEntPrivateData(edict_t *pEnt)
if (!pEntity)
return;

#ifdef REGAMEDLL_FIXES
// FIXED: Ensure this item will be removed from the owner player's inventory 'm_rgpPlayerItems[]'
// to avoid dangling pointers
CBasePlayerItem *pItem = dynamic_cast<CBasePlayerItem *>(pEntity);
if (pItem)
{
CBasePlayer *pOwner = GET_PRIVATE<CBasePlayer>(pItem->pev->owner);
if (pOwner && pOwner->IsPlayer())
{
if (pOwner->m_pActiveItem == pItem && pItem->IsWeapon())
((CBasePlayerWeapon *)pItem)->RetireWeapon();

if (pOwner->RemovePlayerItem(pItem))
{
// Ammo must be dropped, otherwise grenades cannot be buy or picked up
if (IsGrenadeWeapon(pItem->m_iId) || pItem->m_iId == WEAPON_C4)
pOwner->m_rgAmmo[pItem->PrimaryAmmoIndex()] = 0;

pOwner->pev->weapons &= ~(1 << pItem->m_iId);
}
}
}
#endif

#ifdef REGAMEDLL_API
pEntity->OnDestroy();
#endif
Expand Down

0 comments on commit 77a3f2b

Please sign in to comment.