From 6dd60afe238e2d7e403568cc286e8f1152b138a0 Mon Sep 17 00:00:00 2001 From: DeaDDooMER Date: Wed, 28 Dec 2022 18:16:12 +0300 Subject: [PATCH] gl: properly free memory --- src/game/renders/opengl/r_common.pas | 31 ++++++-- src/game/renders/opengl/r_console.pas | 4 +- src/game/renders/opengl/r_gui.pas | 34 ++++----- src/game/renders/opengl/r_loadscreen.pas | 8 +- src/game/renders/opengl/r_map.pas | 93 +++++------------------- src/game/renders/opengl/r_render.pas | 38 +++++----- src/game/renders/opengl/r_textures.pas | 9 ++- 7 files changed, 90 insertions(+), 127 deletions(-) diff --git a/src/game/renders/opengl/r_common.pas b/src/game/renders/opengl/r_common.pas index 174769b..c12cbbb 100644 --- a/src/game/renders/opengl/r_common.pas +++ b/src/game/renders/opengl/r_common.pas @@ -39,6 +39,9 @@ interface var r_Common_ProcessLoadingCallback: TProcedure; + procedure r_Common_FreeAndNil (var obj); + procedure r_Common_FreeMemAndNil (var p); + function r_Common_LoadThis (const name: AnsiString; var here: THereTexture): Boolean; procedure r_Common_FreeThis (var here: THereTexture); @@ -90,6 +93,24 @@ implementation var BackgroundTexture: THereTexture; + procedure r_Common_FreeAndNil (var obj); + var temp: TObject; + begin + temp := TObject(obj); + Pointer(obj) := nil; + if temp <> nil then + temp.Free; + end; + + procedure r_Common_FreeMemAndNil (var p); + var temp: Pointer; + begin + temp := Pointer(p); + Pointer(p) := nil; + if temp <> nil then + FreeMem(temp) + end; + procedure r_Common_GetObjectPos (const obj: TObj; out x, y: Integer); var fx, fy: Integer; begin @@ -299,9 +320,7 @@ implementation procedure r_Common_FreeThis (var here: THereTexture); begin here.name := ''; - if here.id <> nil then - here.id.Free; - here.id := nil; + r_Common_FreeAndNil(here.id); end; function r_Common_LoadThis (const name: AnsiString; var here: THereTexture): Boolean; @@ -381,9 +400,9 @@ implementation procedure r_Common_Free; begin r_Common_FreeThis(BackgroundTexture); - menufont.Free; - smallfont.Free; - stdfont.Free; + FreeAndNil(menufont); + FreeAndNil(smallfont); + FreeAndNil(stdfont); end; (* --------- Loading screen helpers --------- *) diff --git a/src/game/renders/opengl/r_console.pas b/src/game/renders/opengl/r_console.pas index 4487afb..e5adc32 100644 --- a/src/game/renders/opengl/r_console.pas +++ b/src/game/renders/opengl/r_console.pas @@ -29,7 +29,7 @@ interface implementation uses - Math, utils, conbuf, + Math, SysUtils, utils, conbuf, g_game, g_options, g_console, g_language, r_draw, r_textures, r_fonts, r_common ; @@ -62,7 +62,7 @@ implementation procedure r_Console_Free; begin - Background.Free; + r_Common_FreeAndNil(Background); end; procedure r_Console_Update; diff --git a/src/game/renders/opengl/r_gui.pas b/src/game/renders/opengl/r_gui.pas index b6fa9d6..04d3c3c 100644 --- a/src/game/renders/opengl/r_gui.pas +++ b/src/game/renders/opengl/r_gui.pas @@ -96,29 +96,29 @@ implementation Font[FALSE] := nil; Font[TRUE] := nil; - MarkerID[FALSE].Free; - MarkerID[TRUE].Free; + r_Common_FreeAndNil(MarkerID[FALSE]); + r_Common_FreeAndNil(MarkerID[TRUE]); for i := 0 to 8 do - Box[i].Free; + r_Common_FreeAndNil(Box[i]); - ScrollLeft.Free; - ScrollRight.Free; - ScrollMiddle.Free; - ScrollMarker.Free; + r_Common_FreeAndNil(ScrollLeft); + r_Common_FreeAndNil(ScrollRight); + r_Common_FreeAndNil(ScrollMiddle); + r_Common_FreeAndNil(ScrollMarker); - EditLeft.Free; - EditRight.Free; - EditMiddle.Free; + r_Common_FreeAndNil(EditLeft); + r_Common_FreeAndNil(EditRight); + r_Common_FreeAndNil(EditMiddle); - BScrollUp[true].Free; - BScrollUp[false].Free; - BScrollDown[true].Free; - BScrollDown[false].Free; - BScrollMiddle.Free; + r_Common_FreeAndNil(BScrollUp[true]); + r_Common_FreeAndNil(BScrollUp[false]); + r_Common_FreeAndNil(BScrollDown[true]); + r_Common_FreeAndNil(BScrollDown[false]); + r_Common_FreeAndNil(BScrollMiddle); - LogoTex.Free; - nopic.Free; + r_Common_FreeAndNil(LogoTex); + r_Common_FreeAndNil(nopic); r_Common_FreeThis(Background); r_Common_FreeThis(ImageControl); diff --git a/src/game/renders/opengl/r_loadscreen.pas b/src/game/renders/opengl/r_loadscreen.pas index 8b9ce80..bc2f236 100644 --- a/src/game/renders/opengl/r_loadscreen.pas +++ b/src/game/renders/opengl/r_loadscreen.pas @@ -71,10 +71,10 @@ implementation procedure r_LoadScreen_Free; begin - BarL.Free; - BarM.Free; - BarR.Free; - BarF.Free; + r_Common_FreeAndNil(BarL); + r_Common_FreeAndNil(BarM); + r_Common_FreeAndNil(BarR); + r_Common_FreeAndNil(BarF); end; procedure r_LoadScreen_DrawLoadingBar (x0, x1, y, val, maxval: Integer); diff --git a/src/game/renders/opengl/r_map.pas b/src/game/renders/opengl/r_map.pas index 2ca87d7..95c76a3 100644 --- a/src/game/renders/opengl/r_map.pas +++ b/src/game/renders/opengl/r_map.pas @@ -300,7 +300,7 @@ implementation procedure r_Map_Finalize; begin - plist.Free; + r_Common_FreeAndNil(plist); FlagFrame := 0; end; @@ -311,21 +311,17 @@ implementation begin for a := A_STAND to A_LAST do begin - if Models[i].anim[d, a].base <> nil then - Models[i].anim[d, a].base.Free; - if Models[i].anim[d, a].mask <> nil then - Models[i].anim[d, a].mask.Free; - Models[i].anim[d, a].base := nil; - Models[i].anim[d, a].mask := nil; + r_Common_FreeAndNil(Models[i].anim[d, a].base); + r_Common_FreeAndNil(Models[i].anim[d, a].mask); end; end; {$IFDEF ENABLE_GIBS} if Models[i].gibs.base <> nil then for a := 0 to High(Models[i].gibs.base) do - Models[i].gibs.base[a].Free; + r_Common_FreeAndNil(Models[i].gibs.base[a]); if Models[i].gibs.mask <> nil then for a := 0 to High(Models[i].gibs.mask) do - Models[i].gibs.mask[a].Free; + r_Common_FreeAndNil(Models[i].gibs.mask[a]); Models[i].gibs.base := nil; Models[i].gibs.mask := nil; Models[i].gibs.rect := nil; @@ -496,84 +492,38 @@ implementation procedure r_Map_Free; var i, j, k: Integer; d: TDirection; b: Boolean; begin - if TalkTexture <> nil then - TalkTexture.Free; - if IndicatorTexture <> nil then - IndicatorTexture.Free; - if InvulPenta <> nil then - InvulPenta.Free; - InvulPenta := nil; + r_Common_FreeAndNil(TalkTexture); + r_Common_FreeAndNil(IndicatorTexture); + r_Common_FreeAndNil(InvulPenta); for b := false to true do - begin for i := 0 to 2 do - begin - if PunchTextures[b, i] <> nil then - PunchTextures[b, i].Free; - PunchTextures[b, i] := nil; - end; - end; + r_Common_FreeAndNil(PunchTextures[b, i]); {$IFDEF ENABLE_SHELLS} for i := 0 to SHELL_LAST do - begin - if ShellTextures[i] <> nil then - ShellTextures[i].Free; - ShellTextures[i] := nil; - end; + r_Common_FreeAndNil(ShellTextures[i]); {$ENDIF} for i := 0 to FLAG_LAST do - begin - if FlagTextures[i] <> nil then - FlagTextures[i].Free; - FlagTextures[i] := nil; - end; + r_Common_FreeAndNil(FlagTextures[i]); for i := 0 to WEAPON_LAST do - begin - if ShotTextures[i] <> nil then - ShotTextures[i].Free; - ShotTextures[i] := nil; - end; + r_Common_FreeAndNil(ShotTextures[i]); {$IFDEF ENABLE_GFX} - gfxlist := nil; + SetLength(gfxlist, 0); for i := 0 to R_GFX_LAST do - begin - if GFXTextures[i] <> nil then - GFXTextures[i].Free; - GFXTextures[i] := nil; - end; + r_Common_FreeAndNil(GFXTextures[i]); {$ENDIF} for i := 1 to WP_LAST do - begin for j := 0 to W_POS_LAST do - begin for k := 0 to W_ACT_LAST do - begin - if WeapTextures[i, j, k] <> nil then - WeapTextures[i, j, k].Free; - WeapTextures[i, j, k] := nil; - end; - end; - end; + r_Common_FreeAndNil(WeapTextures[i, j, k]); if Models <> nil then for i := 0 to High(Models) do r_Map_FreeModel(i); for i := MONSTER_DEMON to MONSTER_MAN do - begin for j := 0 to ANIM_LAST do - begin for d := TDirection.D_LEFT to TDirection.D_RIGHT do - begin - if MonTextures[i, j, d] <> nil then - MonTextures[i, j, d].Free; - MonTextures[i, j, d] := nil; - end; - end; - end; + r_Common_FreeAndNil(MonTextures[i, j, d]); for i := 0 to ITEM_LAST do - begin - if Items[i].tex <> nil then - Items[i].tex.Free; - Items[i].tex := nil; - end; + r_Common_FreeAndNil(Items[i].tex); end; procedure r_Map_LoadTextures; @@ -616,14 +566,11 @@ implementation var i: Integer; begin plist.Clear; - if SkyTexture <> nil then - SkyTexture.Free; - SkyTexture := nil; + r_Common_FreeAndNil(SkyTexture); if RenTextures <> nil then for i := 0 to High(RenTextures) do - if RenTextures[i].tex <> nil then - RenTextures[i].tex.Free; - RenTextures := nil; + r_Common_FreeAndNil(RenTextures[i].tex); + SetLength(RenTextures, 0); end; procedure r_Map_DrawPanel (p: TPanel); diff --git a/src/game/renders/opengl/r_render.pas b/src/game/renders/opengl/r_render.pas index 5a6bb27..4c1f097 100644 --- a/src/game/renders/opengl/r_render.pas +++ b/src/game/renders/opengl/r_render.pas @@ -160,28 +160,24 @@ implementation {$ENDIF} r_Map_Free; r_Console_Free; - hudbflagd.Free; - hudbflags.Free; - hudbflag.Free; - hudrflagd.Free; - hudrflags.Free; - hudrflag.Free; - hudjet.Free; - hudair.Free; - hudkey[0].Free; - hudkey[1].Free; - hudkey[2].Free; + r_Common_FreeAndNil(hudbflagd); + r_Common_FreeAndNil(hudbflags); + r_Common_FreeAndNil(hudbflag); + r_Common_FreeAndNil(hudrflagd); + r_Common_FreeAndNil(hudrflags); + r_Common_FreeAndNil(hudrflag); + r_Common_FreeAndNil(hudjet); + r_Common_FreeAndNil(hudair); + r_Common_FreeAndNil(hudkey[0]); + r_Common_FreeAndNil(hudkey[1]); + r_Common_FreeAndNil(hudkey[2]); for i := 0 to WP_LAST do - begin - if hudwp[i] <> nil then - hudwp[i].Free; - hudwp[i] := nil; - end; - hudap.Free; - hudhp[true].Free; - hudhp[false].Free; - hudbg.Free; - hud.Free; + r_Common_FreeAndNil(hudwp[i]); + r_Common_FreeAndNil(hudap); + r_Common_FreeAndNil(hudhp[true]); + r_Common_FreeAndNil(hudhp[false]); + r_Common_FreeAndNil(hudbg); + r_Common_FreeAndNil(hud); r_Common_Free; end; diff --git a/src/game/renders/opengl/r_textures.pas b/src/game/renders/opengl/r_textures.pas index aa3bbff..24ea164 100644 --- a/src/game/renders/opengl/r_textures.pas +++ b/src/game/renders/opengl/r_textures.pas @@ -138,6 +138,7 @@ implementation uses SysUtils, Classes, + r_common, e_log, e_res, WADReader, Config, g_console, // cvar declaration Imaging, ImagingTypes, ImagingUtility @@ -331,8 +332,8 @@ implementation var i: Integer; begin for i := 0 to self.count - 1 do - self.mTexture[i].Free; - self.mTexture := nil; + r_Common_FreeAndNil(self.mTexture[i]); + SetLength(self.mTexture, 0); inherited; end; @@ -413,10 +414,10 @@ implementation begin glDeleteTextures(1, @atl[i].id); atl[i].id := 0; - atl[i].Free; + r_Common_FreeAndNil(atl[i]); end; - atl := nil; end; + SetLength(atl, 0); end; function r_Textures_FixImageData (var img: TImageData): Boolean; -- 2.29.2