Use FreeAndNil across the board. Even if we are immediately assigning after, probably avoids accidental mistakes. Also free neglected owner tex on shutdown, and delete hog gears using the normal deletion procedure if for any reason they still exist (EndGame call?).
authornemo
Mon, 08 Dec 2014 09:35:14 -0500
changeset 10634 35d059bd0932
parent 10633 2f062fac5791
child 10635 8e8b320eefad
Use FreeAndNil across the board. Even if we are immediately assigning after, probably avoids accidental mistakes. Also free neglected owner tex on shutdown, and delete hog gears using the normal deletion procedure if for any reason they still exist (EndGame call?).
hedgewars/uCaptions.pas
hedgewars/uChat.pas
hedgewars/uGears.pas
hedgewars/uGearsHandlersMess.pas
hedgewars/uGearsList.pas
hedgewars/uGearsUtils.pas
hedgewars/uLandTexture.pas
hedgewars/uScript.pas
hedgewars/uStore.pas
hedgewars/uTeams.pas
hedgewars/uTextures.pas
hedgewars/uTypes.pas
hedgewars/uVisualGearsList.pas
hedgewars/uWorld.pas
--- a/hedgewars/uCaptions.pas	Sun Dec 07 14:36:28 2014 -0500
+++ b/hedgewars/uCaptions.pas	Mon Dec 08 09:35:14 2014 -0500
@@ -48,10 +48,7 @@
     if Length(s) = 0 then
         exit;
     if Captions[Group].Text <> s then
-        begin
-        FreeTexture(Captions[Group].Tex);
-        Captions[Group].Tex:= nil
-        end;
+        FreeAndNilTexture(Captions[Group].Tex);
 
     if Captions[Group].Tex = nil then
         begin
@@ -73,7 +70,7 @@
 begin
 for Group:= Low(TCapGroup) to High(TCapGroup) do
     if unload then
-        FreeTexture(Captions[Group].Tex)
+        FreeAndNilTexture(Captions[Group].Tex)
     else if length(Captions[Group].Text) > 0 then
         Captions[Group].Tex:= RenderStringTex(Captions[Group].Text, Captions[Group].Color, fntBig)
 end;
@@ -97,8 +94,7 @@
                 inc(offset, Tex^.h + 2);
                 if EndTime <= RealTicks then
                     begin
-                    FreeTexture(Tex);
-                    Tex:= nil;
+                    FreeAndNilTexture(Tex);
                     Text:= ansistring('');
                     EndTime:= 0
                     end;
@@ -114,10 +110,7 @@
 var group: TCapGroup;
 begin
     for group:= Low(TCapGroup) to High(TCapGroup) do
-        begin
-        FreeTexture(Captions[group].Tex);
-        Captions[group].Tex:= nil
-        end
+        FreeAndNilTexture(Captions[group].Tex);
 end;
 
 end.
--- a/hedgewars/uChat.pas	Sun Dec 07 14:36:28 2014 -0500
+++ b/hedgewars/uChat.pas	Mon Dec 08 09:35:14 2014 -0500
@@ -147,7 +147,7 @@
 var color  : TSDL_Color;
 begin
 if cl.Tex <> nil then
-    FreeTexture(cl.Tex);
+    FreeAndNilTexture(cl.Tex);
 
 if isInput then
     begin
@@ -583,9 +583,9 @@
 procedure freeModule;
 var i: ShortInt;
 begin
-    FreeTexture(InputStr.Tex);
+    FreeAndNilTexture(InputStr.Tex);
     for i:= 0 to MaxStrIndex do
-        FreeTexture(Strs[i].Tex);
+        FreeAndNilTexture(Strs[i].Tex);
 end;
 
 end.
--- a/hedgewars/uGears.pas	Sun Dec 07 14:36:28 2014 -0500
+++ b/hedgewars/uGears.pas	Mon Dec 08 09:35:14 2014 -0500
@@ -215,7 +215,7 @@
             begin
             if curHandledGear^.RenderTimer and (curHandledGear^.Timer > 500) and ((curHandledGear^.Timer mod 1000) = 0) then
                 begin
-                FreeTexture(curHandledGear^.Tex);
+                FreeAndNilTexture(curHandledGear^.Tex);
                 curHandledGear^.Tex:= RenderStringTex(ansistring(inttostr(curHandledGear^.Timer div 1000)), cWhiteColor, fntSmall);
                 end;
             curHandledGear^.doStep(curHandledGear);
--- a/hedgewars/uGearsHandlersMess.pas	Sun Dec 07 14:36:28 2014 -0500
+++ b/hedgewars/uGearsHandlersMess.pas	Mon Dec 08 09:35:14 2014 -0500
@@ -3645,7 +3645,7 @@
         begin
         Gear^.Damage:= i;
         //AddCaption('Fuel: '+inttostr(round(Gear^.Health/20))+'%', cWhiteColor, capgrpAmmostate);
-        FreeTexture(Gear^.Tex);
+        FreeAndNilTexture(Gear^.Tex);
         Gear^.Tex := RenderStringTex(trmsg[sidFuel] + ansistring(': ' + inttostr(i) + '%'), cWhiteColor, fntSmall)
         end;
 
@@ -4805,7 +4805,7 @@
         if (i <> Gear^.Damage) and ((GameTicks and $3F) = 0) then
             begin
             Gear^.Damage:= i;
-            FreeTexture(Gear^.Tex);
+            FreeAndNilTexture(Gear^.Tex);
             Gear^.Tex := RenderStringTex(trmsg[sidFuel] + ansistring(': ' + inttostr(i) +
                          '%'), cWhiteColor, fntSmall)
             end
@@ -4882,7 +4882,7 @@
         if (i <> Gear^.Damage) and ((GameTicks and $3F) = 0) then
             begin
             Gear^.Damage:= i;
-            FreeTexture(Gear^.Tex);
+            FreeAndNilTexture(Gear^.Tex);
             Gear^.Tex := RenderStringTex(trmsg[sidFuel] + ansistring(': ' + inttostr(i) +
                          '%'), cWhiteColor, fntSmall)
             end
@@ -5487,7 +5487,7 @@
     if (t <> Gear^.Damage) and ((GameTicks and $3F) = 0) then
     begin
     Gear^.Damage:= t;
-    FreeTexture(Gear^.Tex);
+    FreeAndNilTexture(Gear^.Tex);
     Gear^.Tex := RenderStringTex(trmsg[sidFuel] + ansistring(': ' + inttostr(t) +
               '%'), cWhiteColor, fntSmall)
     end;
--- a/hedgewars/uGearsList.pas	Sun Dec 07 14:36:28 2014 -0500
+++ b/hedgewars/uGearsList.pas	Mon Dec 08 09:35:14 2014 -0500
@@ -143,14 +143,22 @@
 
 procedure RemoveGearFromList(Gear: PGear);
 begin
-TryDo((curHandledGear = nil) or (Gear = curHandledGear), 'You''re doing it wrong', true);
+if (Gear <> GearsList) and (Gear <> nil) and (Gear^.NextGear = nil) and (Gear^.PrevGear = nil) then
+    begin
+    AddFileLog('Attempted to remove Gear #'+inttostr(Gear^.uid)+' from the list twice.');
+    exit
+    end;
+TryDo((Gear = nil) or (curHandledGear = nil) or (Gear = curHandledGear), 'You''re doing it wrong', true);
 
 if Gear^.NextGear <> nil then
     Gear^.NextGear^.PrevGear:= Gear^.PrevGear;
 if Gear^.PrevGear <> nil then
     Gear^.PrevGear^.NextGear:= Gear^.NextGear
-else
-    GearsList:= Gear^.NextGear
+else 
+    GearsList:= Gear^.NextGear;
+
+Gear^.NextGear:= nil;
+Gear^.PrevGear:= nil
 end;
 
 
@@ -631,8 +639,7 @@
 
 DeleteCI(Gear);
 
-FreeTexture(Gear^.Tex);
-Gear^.Tex:= nil;
+FreeAndNilTexture(Gear^.Tex);
 
 // make sure that portals have their link removed before deletion
 if (Gear^.Kind = gtPortal) then
@@ -704,7 +711,7 @@
             with CurrentHedgehog^ do
                 begin
                 inc(Team^.stats.AIKills);
-                FreeTexture(Team^.AIKillsTex);
+                FreeAndNilTexture(Team^.AIKillsTex);
                 Team^.AIKillsTex := RenderStringTex(ansistring(inttostr(Team^.stats.AIKills)), Team^.Clan^.Color, fnt16);
                 end
         end;
@@ -719,7 +726,9 @@
     FollowGear:= nil;
 if lastGearByUID = Gear then
     lastGearByUID := nil;
-RemoveGearFromList(Gear);
+if Gear^.Hedgehog^.GearHidden <> Gear then // hidden hedgehogs shouldn't be in the list
+    RemoveGearFromList(Gear);
+Gear^.Hedgehog^.GearHidden:= nil;
 Dispose(Gear)
 end;
 
--- a/hedgewars/uGearsUtils.pas	Sun Dec 07 14:36:28 2014 -0500
+++ b/hedgewars/uGearsUtils.pas	Mon Dec 08 09:35:14 2014 -0500
@@ -700,7 +700,7 @@
         with CurrentHedgehog^ do
             begin
             inc(Team^.stats.AIKills);
-            FreeTexture(Team^.AIKillsTex);
+            FreeAndNilTexture(Team^.AIKillsTex);
             Team^.AIKillsTex := RenderStringTex(ansistring(inttostr(Team^.stats.AIKills)), Team^.Clan^.Color, fnt16);
             end;
     tempTeam := gear^.Hedgehog^.Team;
--- a/hedgewars/uLandTexture.pas	Sun Dec 07 14:36:28 2014 -0500
+++ b/hedgewars/uLandTexture.pas	Mon Dec 08 09:35:14 2014 -0500
@@ -169,10 +169,7 @@
                         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, TEXSIZE, TEXSIZE, 0, GL_RGBA, GL_UNSIGNED_BYTE, Pixels(x,y));
                         end
                     else if tex <> nil then
-                        begin
-                        FreeTexture(tex);
-                        tex:= nil
-                        end;
+                        FreeAndNilTexture(tex);
 
                     // nothing else to do
                     if dirtyLandTexCount < 1 then
@@ -284,13 +281,8 @@
     for x:= 0 to LANDTEXARW - 1 do
         for y:= 0 to LANDTEXARH - 1 do
             with LandTextures[x, y] do
-                begin
                 if tex <> nil then
-                    begin
-                    FreeTexture(tex);
-                    tex:= nil
-                    end
-                end;
+                    FreeAndNilTexture(tex)
 end;
 
 procedure freeModule;
--- a/hedgewars/uScript.pas	Sun Dec 07 14:36:28 2014 -0500
+++ b/hedgewars/uScript.pas	Mon Dec 08 09:35:14 2014 -0500
@@ -1069,16 +1069,16 @@
                 hh:= team^.Hedgehogs[j];
                 if (hh.Gear <> nil) or (hh.GearHidden <> nil) then
                     begin
-                    FreeTexture(hh.NameTagTex);
+                    FreeAndNilTexture(hh.NameTagTex);
                     hh.NameTagTex:= RenderStringTex(ansistring(hh.Name), clan^.Color, fnt16);
                     RenderHealth(hh);
                     end;
                 end;
-            FreeTexture(team^.NameTagTex);
+            FreeAndNilTexture(team^.NameTagTex);
             team^.NameTagTex:= RenderStringTex(ansistring(clan^.Teams[i]^.TeamName), clan^.Color, fnt16);
             end;
 
-	FreeTexture(clan^.HealthTex);
+	    FreeAndNilTexture(clan^.HealthTex);
         clan^.HealthTex:= makeHealthBarTexture(cTeamHealthWidth + 5, clan^.Teams[0]^.NameTagTex^.h, clan^.Color);
         end;
 
@@ -1111,7 +1111,7 @@
             begin
             gear^.Hedgehog^.Team^.TeamName := lua_tostring(L, 2);
 
-            FreeTexture(gear^.Hedgehog^.Team^.NameTagTex);
+            FreeAndNilTexture(gear^.Hedgehog^.Team^.NameTagTex);
             gear^.Hedgehog^.Team^.NameTagTex:= RenderStringTex(ansistring(gear^.Hedgehog^.Team^.TeamName), gear^.Hedgehog^.Team^.Clan^.Color, fnt16);
             end
         else
@@ -1150,7 +1150,7 @@
             begin
             gear^.Hedgehog^.Name:= lua_tostring(L, 2);
 
-            FreeTexture(gear^.Hedgehog^.NameTagTex);
+            FreeAndNilTexture(gear^.Hedgehog^.NameTagTex);
             gear^.Hedgehog^.NameTagTex:= RenderStringTex(ansistring(gear^.Hedgehog^.Name), gear^.Hedgehog^.Team^.Clan^.Color, fnt16)
             end
         end;
--- a/hedgewars/uStore.pas	Sun Dec 07 14:36:28 2014 -0500
+++ b/hedgewars/uStore.pas	Mon Dec 08 09:35:14 2014 -0500
@@ -50,7 +50,6 @@
 function  RenderHelpWindow(caption, subcaption, description, extra: ansistring; extracolor: LongInt; iconsurf: PSDL_Surface; iconrect: PSDL_Rect): PTexture;
 procedure RenderWeaponTooltip(atype: TAmmoType);
 procedure ShowWeaponTooltip(x, y: LongInt);
-procedure FreeWeaponTooltip;
 procedure MakeCrossHairs;
 {$IFDEF USE_VIDEO_RECORDING}
 procedure InitOffscreenOpenGL;
@@ -474,7 +473,7 @@
             tmpsurf:= TTF_RenderUTF8_Blended(Fontz[CheckCJKFont(trAmmo[NameId],fnt16)].Handle, PChar(trAmmo[NameId]), cWhiteColorChannels);
             TryDo(tmpsurf <> nil,'Name-texture creation for ammo type #' + intToStr(ord(ai)) + ' failed!',true);
             tmpsurf:= doSurfaceConversion(tmpsurf);
-            FreeTexture(NameTex);
+            FreeAndNilTexture(NameTex);
             NameTex:= Surface2Tex(tmpsurf, false);
             SDL_FreeSurface(tmpsurf)
             end;
@@ -484,7 +483,7 @@
         begin
         tmpsurf:= TTF_RenderUTF8_Blended(Fontz[fnt16].Handle, Str2PChar(IntToStr(i) + 'x'), cWhiteColorChannels);
         tmpsurf:= doSurfaceConversion(tmpsurf);
-        FreeTexture(CountTexz[i]);
+        FreeAndNilTexture(CountTexz[i]);
         CountTexz[i]:= Surface2Tex(tmpsurf, false);
         SDL_FreeSurface(tmpsurf)
         end;
@@ -579,7 +578,7 @@
 var s: shortstring;
 begin
 str(Hedgehog.Gear^.Health, s);
-FreeTexture(Hedgehog.HealthTagTex);
+FreeAndNilTexture(Hedgehog.HealthTagTex);
 Hedgehog.HealthTagTex:= RenderStringTex(ansistring(s), Hedgehog.Team^.Clan^.Color, fnt16)
 end;
 
@@ -696,10 +695,7 @@
 begin
     // free the mem of any previously assigned texture.  This was previously only if the new one could be loaded, but, NoHat is usually a better choice
     if HH.HatTex <> nil then
-        begin
-        FreeTexture(HH.HatTex);
-        HH.HatTex:= nil
-        end;
+        FreeAndNilTexture(HH.HatTex);
 
     // load new hat surface if this hat is different than the one already loaded
     if newHat <> prevHat then
@@ -842,8 +838,7 @@
             GameLoaded();
     {$ENDIF}
     WriteLnToConsole('Freeing progress surface... ');
-    FreeTexture(ProgrTex);
-    ProgrTex:= nil;
+    FreeAndNilTexture(ProgrTex);
     Step:= 0
 end;
 
@@ -981,7 +976,7 @@
         end;
 
 // free old texture
-FreeWeaponTooltip;
+FreeAndNilTexture(WeaponTooltipTex);
 
 // image region
 i:= LongInt(atype) - 1;
@@ -1021,13 +1016,6 @@
     DrawTexture(x, y, WeaponTooltipTex)
 end;
 
-procedure FreeWeaponTooltip;
-begin
-// free the existing texture (if there is any)
-FreeTexture(WeaponTooltipTex);
-WeaponTooltipTex:= nil
-end;
-
 {$IFDEF USE_VIDEO_RECORDING}
 {$IFDEF SDL2}
 procedure InitOffscreenOpenGL;
--- a/hedgewars/uTeams.pas	Sun Dec 07 14:36:28 2014 -0500
+++ b/hedgewars/uTeams.pas	Mon Dec 08 09:35:14 2014 -0500
@@ -852,28 +852,31 @@
         for h:= 0 to cMaxHHIndex do
             with TeamsArray[i]^.Hedgehogs[h] do
                 begin
+                if Gear <> nil then
+                    DeleteGear(Gear);
                 if GearHidden <> nil then
-                    Dispose(GearHidden);
+                    DeleteGear(GearHidden);
 
-                FreeTexture(NameTagTex);
-                FreeTexture(HealthTagTex);
-                FreeTexture(HatTex);
+                FreeAndNilTexture(NameTagTex);
+                FreeAndNilTexture(HealthTagTex);
+                FreeAndNilTexture(HatTex)
                 end;
 
         with TeamsArray[i]^ do
             begin
-            FreeTexture(NameTagTex);
-            FreeTexture(GraveTex);
-            FreeTexture(AIKillsTex);
-            FreeTexture(FlagTex);
+            FreeAndNilTexture(NameTagTex);
+            FreeAndNilTexture(OwnerTex);
+            FreeAndNilTexture(GraveTex);
+            FreeAndNilTexture(AIKillsTex);
+            FreeAndNilTexture(FlagTex);
             end;
 
-        Dispose(TeamsArray[i]);
-    end;
+        Dispose(TeamsArray[i])
+        end;
     for i:= 0 to Pred(ClansCount) do
         begin
-        FreeTexture(ClansArray[i]^.HealthTex);
-        Dispose(ClansArray[i]);
+        FreeAndNilTexture(ClansArray[i]^.HealthTex);
+        Dispose(ClansArray[i])
         end
     end;
 TeamsCount:= 0;
--- a/hedgewars/uTextures.pas	Sun Dec 07 14:36:28 2014 -0500
+++ b/hedgewars/uTextures.pas	Mon Dec 08 09:35:14 2014 -0500
@@ -28,7 +28,7 @@
 procedure PrettifySurfaceAlpha(surf: PSDL_Surface; pixels: PLongwordArray);
 procedure PrettifyAlpha2D(pixels: TLandArray; height, width: LongWord);
 procedure FreeTexture(tex: PTexture);
-procedure FreeAndNilTexture(var tex: PTexture);
+procedure FreeAndNilTexture(var tex: PTexture); inline;
 
 procedure initModule;
 procedure freeModule;
@@ -312,7 +312,7 @@
     end
 end;
 
-procedure FreeAndNilTexture(var tex: PTexture);
+procedure FreeAndNilTexture(var tex: PTexture); inline;
 begin
     FreeTexture(tex);
     tex:= nil
--- a/hedgewars/uTypes.pas	Sun Dec 07 14:36:28 2014 -0500
+++ b/hedgewars/uTypes.pas	Mon Dec 08 09:35:14 2014 -0500
@@ -388,7 +388,7 @@
             Hedgehogs: array[0..cMaxHHIndex] of THedgehog;
             CurrHedgehog: LongWord;
             NameTagTex,
-            OwnerTex: PTexture;
+            OwnerTex,
             GraveTex,
             AIKillsTex,
             FlagTex: PTexture;
--- a/hedgewars/uVisualGearsList.pas	Sun Dec 07 14:36:28 2014 -0500
+++ b/hedgewars/uVisualGearsList.pas	Mon Dec 08 09:35:14 2014 -0500
@@ -432,8 +432,7 @@
 
 procedure DeleteVisualGear(Gear: PVisualGear);
 begin
-    FreeTexture(Gear^.Tex);
-    Gear^.Tex:= nil;
+    FreeAndNilTexture(Gear^.Tex);
 
     if Gear^.NextGear <> nil then
         Gear^.NextGear^.PrevGear:= Gear^.PrevGear;
--- a/hedgewars/uWorld.pas	Sun Dec 07 14:36:28 2014 -0500
+++ b/hedgewars/uWorld.pas	Mon Dec 08 09:35:14 2014 -0500
@@ -404,17 +404,12 @@
 // for uStore texture resetting
 procedure ResetWorldTex;
 begin
-    FreeTexture(fpsTexture);
-    fpsTexture:= nil;
-    FreeTexture(timeTexture);
-    timeTexture:= nil;
-    FreeTexture(missionTex);
-    missionTex:= nil;
-    FreeTexture(recTexture);
-    recTexture:= nil;
-    FreeTexture(AmmoMenuTex);
+    FreeAndNilTexture(fpsTexture);
+    FreeAndNilTexture(timeTexture);
+    FreeAndNilTexture(missionTex);
+    FreeAndNilTexture(recTexture);
+    FreeAndNilTexture(AmmoMenuTex);
     AmmoMenuInvalidated:= true;
-    AmmoMenuTex:= nil;
 end;
 
 function GetAmmoMenuTexture(Ammo: PHHAmmo): PTexture;
@@ -566,7 +561,7 @@
 if(AmmoMenuInvalidated) then
     begin
     AmmoMenuInvalidated:= false;
-    FreeTexture(AmmoMenuTex);
+    FreeAndNilTexture(AmmoMenuTex);
     AmmoMenuTex:= GetAmmoMenuTexture(Ammo);
 
 {$IFDEF USE_LANDSCAPE_AMMOMENU}
@@ -749,7 +744,7 @@
                 bShowAmmoMenu:= false;
                 SetWeapon(Ammo^[Slot, Pos].AmmoType);
                 bSelected:= false;
-                FreeWeaponTooltip;
+                FreeAndNilTexture(WeaponTooltipTex);
 {$IFDEF USE_TOUCH_INTERFACE}//show the aiming buttons + animation
                 if (Ammo^[Slot, Pos].Propz and ammoprop_NeedUpDown) <> 0 then
                     begin
@@ -772,7 +767,7 @@
             end
         end
     else
-        FreeWeaponTooltip;
+        FreeAndNilTexture(WeaponTooltipTex);
 
     if (WeaponTooltipTex <> nil) and (AMShiftX = 0) and (AMShiftY = 0) then
 {$IFDEF USE_LANDSCAPE_AMMOMENU}
@@ -1682,7 +1677,7 @@
 
         tmpSurface:= TTF_RenderUTF8_Blended(Fontz[fnt16].Handle, Str2PChar(s), cWhiteColorChannels);
         tmpSurface:= doSurfaceConversion(tmpSurface);
-        FreeTexture(timeTexture);
+        FreeAndNilTexture(timeTexture);
         timeTexture:= Surface2Tex(tmpSurface, false);
         SDL_FreeSurface(tmpSurface)
         end;
@@ -1700,7 +1695,7 @@
             s:= inttostr(FPS) + ' fps';
             tmpSurface:= TTF_RenderUTF8_Blended(Fontz[fnt16].Handle, Str2PChar(s), cWhiteColorChannels);
             tmpSurface:= doSurfaceConversion(tmpSurface);
-            FreeTexture(fpsTexture);
+            FreeAndNilTexture(fpsTexture);
             fpsTexture:= Surface2Tex(tmpSurface, false);
             SDL_FreeSurface(tmpSurface)
             end;
@@ -1763,7 +1758,7 @@
         s:= 'rec';
         tmpSurface:= TTF_RenderUTF8_Blended(Fontz[fntBig].Handle, Str2PChar(s), cWhiteColorChannels);
         tmpSurface:= doSurfaceConversion(tmpSurface);
-        FreeTexture(recTexture);
+        FreeAndNilTexture(recTexture);
         recTexture:= Surface2Tex(tmpSurface, false);
         SDL_FreeSurface(tmpSurface)
         end;
@@ -2001,7 +1996,7 @@
 if time = 0 then
     time:= 5000;
 missionTimer:= time;
-FreeTexture(missionTex);
+FreeAndNilTexture(missionTex);
 
 if icon > -1 then
     begin