Hammer had been accessing dangling pointers under particular circumstances (e.g. when hitting a mine/barrel right when it exploded)
authorsheepluva
Sun, 09 Aug 2015 04:30:41 +0200
changeset 11043 79a77bef4c61
parent 11042 d29a05a55bbc
child 11044 c3d76daae637
Hammer had been accessing dangling pointers under particular circumstances (e.g. when hitting a mine/barrel right when it exploded)
hedgewars/uGearsList.pas
--- a/hedgewars/uGearsList.pas	Sun Aug 09 04:02:23 2015 +0200
+++ b/hedgewars/uGearsList.pas	Sun Aug 09 04:30:41 2015 +0200
@@ -155,7 +155,7 @@
     Gear^.NextGear^.PrevGear:= Gear^.PrevGear;
 if Gear^.PrevGear <> nil then
     Gear^.PrevGear^.NextGear:= Gear^.NextGear
-else 
+else
     GearsList:= Gear^.NextGear;
 
 Gear^.NextGear:= nil;
@@ -671,6 +671,7 @@
     t,i: Longword;
     k: boolean;
     cakeData: PCakeData;
+    iterator: PGear;
 begin
 
 ScriptCall('onGearDelete', gear^.uid);
@@ -679,6 +680,31 @@
 
 FreeAndNilTexture(Gear^.Tex);
 
+// remove potential links to this gear
+// currently relevant to: gears linked by hammer
+if (Gear^.Kind = gtHedgehog) or (Gear^.Kind = gtMine) or (Gear^.Kind = gtExplosives) then
+    begin
+    // check all gears for stuff to port through
+    iterator := nil;
+    while true do
+        begin
+
+        // iterate through GearsList
+        if iterator = nil then
+            iterator := GearsList
+        else
+            iterator := iterator^.NextGear;
+
+        // end of list?
+        if iterator = nil then
+            break;
+
+        if iterator^.LinkedGear = Gear then
+            iterator^.LinkedGear:= nil;
+        end;
+
+    end;
+
 // make sure that portals have their link removed before deletion
 if (Gear^.Kind = gtPortal) then
     begin