From 7f740c2f67621ddaa9ce12806851026ec70090b9 Mon Sep 17 00:00:00 2001 From: Joonas Rikkonen Date: Mon, 26 Feb 2018 15:06:00 +0200 Subject: [PATCH] Attempt to diagnose issues #278, #284 and #291. It appears that the game attempts to access the Limbs-field of a ragdoll after it's been set to null (which should only happen when the ragdoll is removed). --- .../Source/Characters/Animation/Ragdoll.cs | 28 ++++++++++++++----- .../Source/Characters/Character.cs | 2 -- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/Barotrauma/BarotraumaShared/Source/Characters/Animation/Ragdoll.cs b/Barotrauma/BarotraumaShared/Source/Characters/Animation/Ragdoll.cs index 9af3cd522..67ef57e13 100644 --- a/Barotrauma/BarotraumaShared/Source/Characters/Animation/Ragdoll.cs +++ b/Barotrauma/BarotraumaShared/Source/Characters/Animation/Ragdoll.cs @@ -13,11 +13,23 @@ namespace Barotrauma { partial class Ragdoll { - public static List list = new List(); + private static List list = new List(); protected Hull currentHull; - public Limb[] Limbs; + private Limb[] limbs; + public Limb[] Limbs + { + get + { + if (limbs == null) + { + DebugConsole.ThrowError("Attempted to access a potentially removed ragdoll. Character: " + character.Name + ", id: " + character.ID + ", removed: " + character.Removed); + return new Limb[0]; + } + return limbs; + } + } private bool frozen; public bool Frozen @@ -281,7 +293,7 @@ namespace Barotrauma float scale = element.GetAttributeFloat("scale", 1.0f); - Limbs = new Limb[element.Elements("limb").Count()]; + limbs = new Limb[element.Elements("limb").Count()]; LimbJoints = new LimbJoint[element.Elements("joint").Count()]; limbDictionary = new Dictionary(); @@ -417,11 +429,13 @@ namespace Barotrauma public void AddLimb(Limb limb) { + if (Limbs.Contains(limb)) return; + limb.body.FarseerBody.OnCollision += OnLimbCollision; - Array.Resize(ref Limbs, Limbs.Length + 1); + Array.Resize(ref limbs, Limbs.Length + 1); - Limbs[Limbs.Length-1] = limb; + Limbs[Limbs.Length - 1] = limb; Mass += limb.Mass; if (!limbDictionary.ContainsKey(limb.type)) limbDictionary.Add(limb.type, limb); @@ -441,7 +455,7 @@ namespace Barotrauma i++; } - Limbs = newLimbs; + limbs = newLimbs; if (limbDictionary.ContainsKey(limb.type)) limbDictionary.Remove(limb.type); //remove all joints that were attached to the removed limb @@ -1501,7 +1515,7 @@ namespace Barotrauma { l.Remove(); } - Limbs = null; + limbs = null; } foreach (PhysicsBody b in collider) diff --git a/Barotrauma/BarotraumaShared/Source/Characters/Character.cs b/Barotrauma/BarotraumaShared/Source/Characters/Character.cs index 1315cb1ef..6dce22117 100644 --- a/Barotrauma/BarotraumaShared/Source/Characters/Character.cs +++ b/Barotrauma/BarotraumaShared/Source/Characters/Character.cs @@ -1981,12 +1981,10 @@ namespace Barotrauma public override void Remove() { -#if DEBUG if (Removed) { DebugConsole.ThrowError("Attempting to remove an already removed character\n" + Environment.StackTrace); } -#endif base.Remove();