From 424a6d90bd154e0b657e394b6dfced585fed01bd Mon Sep 17 00:00:00 2001 From: erorcun Date: Fri, 2 Jul 2021 23:56:31 +0300 Subject: [PATCH] Peds: Efforts to find the reason of crashes on GCC 7.5.0 * Prevent deleted peds to be in nearPeds list (was always needed, might be unrelated to crashes) * Add asserts to nearPeds and PedIK ctor (crash reasons) * Shouldn't fix reported crashes since they were caused by null peds. --- src/peds/Ped.cpp | 28 +++++++++++++++++++++++++++- src/peds/PedIK.cpp | 4 ++-- src/peds/PedIK.h | 2 +- 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/src/peds/Ped.cpp b/src/peds/Ped.cpp index 04e13c33..0acc4daf 100644 --- a/src/peds/Ped.cpp +++ b/src/peds/Ped.cpp @@ -308,6 +308,30 @@ CPed::~CPed(void) m_pFire->Extinguish(); CPopulation::UpdatePedCount((ePedType)m_nPedType, true); DMAudio.DestroyEntity(m_audioEntityId); + + // Because of the nature of ped lists in GTA, it can sometimes be outdated. + // Remove ourself from nearPeds list of the Peds in our nearPeds list. +#ifdef FIX_BUGS + for(int i = 0; i < m_numNearPeds; i++) { + CPed *nearPed = m_nearPeds[i]; + assert(nearPed != nil); + if (!nearPed->IsPointerValid()) + continue; + + for(int j = 0; j < nearPed->m_numNearPeds;) { + assert(j == ARRAY_SIZE(m_nearPeds) - 1 || nearPed->m_nearPeds[j] || !nearPed->m_nearPeds[j+1]); // ensure nil comes after nil + + if (nearPed->m_nearPeds[j] == this) { + for (int k = j; k < ARRAY_SIZE(m_nearPeds) - 1; k++) { + nearPed->m_nearPeds[k] = nearPed->m_nearPeds[k + 1]; + nearPed->m_nearPeds[k + 1] = nil; + } + nearPed->m_numNearPeds--; + } else + j++; + } + } +#endif } void @@ -398,13 +422,15 @@ CPed::BuildPedLists(void) } else removePed = true; } + + assert(i == ARRAY_SIZE(m_nearPeds) - 1 || m_nearPeds[i] || !m_nearPeds[i+1]); // ensure nil comes after nil + if (removePed) { // If we arrive here, the ped we're checking isn't "near", so we should remove it. for (int j = i; j < ARRAY_SIZE(m_nearPeds) - 1; j++) { m_nearPeds[j] = m_nearPeds[j + 1]; m_nearPeds[j + 1] = nil; } - // Above loop won't work on last slot, so we need to empty it. m_nearPeds[ARRAY_SIZE(m_nearPeds) - 1] = nil; m_numNearPeds--; } else diff --git a/src/peds/PedIK.cpp b/src/peds/PedIK.cpp index de2c23ce..8358a196 100644 --- a/src/peds/PedIK.cpp +++ b/src/peds/PedIK.cpp @@ -17,9 +17,9 @@ const RwV3d XaxisIK = { 1.0f, 0.0f, 0.0f}; const RwV3d YaxisIK = { 0.0f, 1.0f, 0.0f}; const RwV3d ZaxisIK = { 0.0f, 0.0f, 1.0f}; -CPedIK::CPedIK(CPed *ped) +CPedIK::CPedIK(CPed *ped) : m_ped(ped) { - m_ped = ped; + assert(ped != nil); m_flags = 0; m_headOrient.yaw = 0.0f; m_headOrient.pitch = 0.0f; diff --git a/src/peds/PedIK.h b/src/peds/PedIK.h index 4eeef6f0..9077fbea 100644 --- a/src/peds/PedIK.h +++ b/src/peds/PedIK.h @@ -34,7 +34,7 @@ public: AIMS_WITH_ARM = 4, }; - CPed *m_ped; + CPed *Const m_ped; LimbOrientation m_headOrient; LimbOrientation m_torsoOrient; LimbOrientation m_upperArmOrient;