From 43033e830ad6f8fc773898e04f7f806d669c5cce Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Tue, 24 Dec 2019 19:47:34 +0100 Subject: [PATCH] - Blood now also starts the first level without leaks. I had to refactor the LoadSave data to allow automatic takedown, the linked list was not the most convenient thing - an array is better. --- source/blood/src/blood.cpp | 11 +++++++---- source/blood/src/blood.h | 1 + source/blood/src/eventq.cpp | 4 ++++ source/blood/src/loadsave.cpp | 11 ++++------- source/blood/src/loadsave.h | 13 ++----------- source/build/include/palette.h | 1 + source/build/src/palette.cpp | 7 +++++++ source/common/gamecontrol.cpp | 1 + source/common/menu/menu.cpp | 6 ++++++ source/common/textures/image.h | 7 +++---- source/platform/win32/i_main.cpp | 2 +- source/platform/win32/st_start.cpp | 2 +- 12 files changed, 38 insertions(+), 28 deletions(-) diff --git a/source/blood/src/blood.cpp b/source/blood/src/blood.cpp index 7bcaddff6..dc169caad 100644 --- a/source/blood/src/blood.cpp +++ b/source/blood/src/blood.cpp @@ -104,8 +104,6 @@ short BloodVersion = 0x115; int gNetPlayers; char *pUserTiles = NULL; -char *pUserSoundRFF = NULL; -char *pUserRFF = NULL; int gChokeCounter = 0; @@ -177,8 +175,6 @@ void ShutDown(void) if (syncstate) printf("A packet was lost! (syncstate)\n"); DO_FREE_AND_NULL(pUserTiles); - DO_FREE_AND_NULL(pUserSoundRFF); - DO_FREE_AND_NULL(pUserRFF); } void QuitGame(void) @@ -1965,6 +1961,13 @@ void sndPlaySpecialMusicOrNothing(int nMusic) } } +extern IniFile* BloodINI; +void GameInterface::FreeGameData() +{ + if (BloodINI) delete BloodINI; + ShutDown(); +} + ::GameInterface* CreateInterface() { diff --git a/source/blood/src/blood.h b/source/blood/src/blood.h index 4a50ec372..b62d90f4e 100644 --- a/source/blood/src/blood.h +++ b/source/blood/src/blood.h @@ -84,6 +84,7 @@ struct GameInterface : ::GameInterface { void faketimerhandler() override; int app_main() override; + void FreeGameData() override; bool validate_hud(int) override; void set_hud_layout(int size) override; void set_hud_scale(int size) override; diff --git a/source/blood/src/eventq.cpp b/source/blood/src/eventq.cpp index 9ea7d78ba..37d286ebd 100644 --- a/source/blood/src/eventq.cpp +++ b/source/blood/src/eventq.cpp @@ -49,6 +49,10 @@ public: { PQueue = NULL; } + ~EventQueue() + { + if (PQueue) delete PQueue; + } bool IsNotEmpty(unsigned int nTime) { return PQueue->Size() > 0 && nTime >= PQueue->LowestPriority(); diff --git a/source/blood/src/loadsave.cpp b/source/blood/src/loadsave.cpp index a42cf95e3..9f3773db7 100644 --- a/source/blood/src/loadsave.cpp +++ b/source/blood/src/loadsave.cpp @@ -60,9 +60,9 @@ unsigned int dword_27AA3C = 0; unsigned int dword_27AA40 = 0; void *dword_27AA44 = NULL; -LoadSave LoadSave::head(123); FileWriter *LoadSave::hSFile = NULL; FileReader LoadSave::hLFile; +TDeletingArray LoadSave::loadSaves; short word_27AA54 = 0; @@ -121,11 +121,10 @@ bool GameInterface::LoadGame(FSaveGameNode* node) LoadSave::hLFile = ReadSavegameChunk("snapshot.bld"); if (!LoadSave::hLFile.isOpen()) return false; - LoadSave *rover = LoadSave::head.next; - while (rover != &LoadSave::head) + + for (auto rover : LoadSave::loadSaves) { rover->Load(); - rover = rover->next; } LoadSave::hLFile.Close(); @@ -193,14 +192,12 @@ bool GameInterface::SaveGame(FSaveGameNode* node) { dword_27AA38 = 0; dword_27AA40 = 0; - LoadSave* rover = LoadSave::head.next; - while (rover != &LoadSave::head) + for (auto rover : LoadSave::loadSaves) { rover->Save(); if (dword_27AA38 > dword_27AA40) dword_27AA40 = dword_27AA38; dword_27AA38 = 0; - rover = rover->next; } } catch (std::runtime_error & err) diff --git a/source/blood/src/loadsave.h b/source/blood/src/loadsave.h index 9dac3b0e4..847970aec 100644 --- a/source/blood/src/loadsave.h +++ b/source/blood/src/loadsave.h @@ -32,18 +32,9 @@ public: static LoadSave head; static FileWriter *hSFile; static FileReader hLFile; - LoadSave *prev; - LoadSave *next; + static TDeletingArray loadSaves; LoadSave() { - prev = head.prev; - prev->next = this; - next = &head; - next->prev = this; - } - LoadSave(int dummy) - { - UNREFERENCED_PARAMETER(dummy); - next = prev = this; + loadSaves.Push(this); } //~LoadSave() { } virtual void Save(void); diff --git a/source/build/include/palette.h b/source/build/include/palette.h index b92140a3a..fb3440d3e 100644 --- a/source/build/include/palette.h +++ b/source/build/include/palette.h @@ -55,6 +55,7 @@ extern void videoFadeToBlack(int32_t moreopaquep); void paletteMakeLookupTable(int32_t palnum, const char *remapbuf, uint8_t r, uint8_t g, uint8_t b, char noFloorPal); void paletteSetColorTable(int32_t id, uint8_t const *table, bool transient = false); void paletteFreeColorTable(int32_t id); +void paletteFreeColorTables(); void paletteSetBlendTable(int32_t blend, const char *tab); void paletteFreeBlendTable(int32_t blend); int32_t paletteSetLookupTable(int32_t palnum, const uint8_t *shtab); diff --git a/source/build/src/palette.cpp b/source/build/src/palette.cpp index 149535007..f9052855a 100644 --- a/source/build/src/palette.cpp +++ b/source/build/src/palette.cpp @@ -729,6 +729,13 @@ void paletteFreeColorTable(int32_t const id) DO_FREE_AND_NULL(basepaltable[id]); } +void paletteFreeColorTables() +{ + for (int i = 0; i < countof(basepaltable); i++) + { + paletteFreeColorTable(i); + } +} // // setbrightness // diff --git a/source/common/gamecontrol.cpp b/source/common/gamecontrol.cpp index 802ed4daf..38a3ae979 100644 --- a/source/common/gamecontrol.cpp +++ b/source/common/gamecontrol.cpp @@ -320,6 +320,7 @@ int GameMain() TileFiles.CloseAll(); // do this before shutting down graphics. GLInterface.Deinit(); I_ShutdownGraphics(); + paletteFreeColorTables(); gi->FreeGameData(); if (gi) delete gi; #ifndef NETCODE_DISABLE diff --git a/source/common/menu/menu.cpp b/source/common/menu/menu.cpp index fd31bb1fb..baf64bc38 100644 --- a/source/common/menu/menu.cpp +++ b/source/common/menu/menu.cpp @@ -422,6 +422,12 @@ void M_ActivateMenu(DMenu *menu) bool M_SetMenu(FName menu, int param, FName caller) { +#if 0 + // skip the menu and go right into the first level. + // For tracking memory leaks that normally require operating the menu to start the game so that they always get the same allocation number. + GameStartupInfo.Episode = GameStartupInfo.Skill = 0; + menu = NAME_StartGame; +#endif if (DrawBackground == -1) { if (menu == NAME_MainMenu) DrawBackground = 1; diff --git a/source/common/textures/image.h b/source/common/textures/image.h index d6bbfffda..4bca8ed2b 100644 --- a/source/common/textures/image.h +++ b/source/common/textures/image.h @@ -123,12 +123,11 @@ class FImageTexture : public FTexture FImageSource *mImage; public: FImageTexture (FImageSource *image, const char *name = nullptr); - void Create8BitPixels(uint8_t* buffer) override; - - void SetImage(FImageSource *img) // This is only for the multipatch texture builder! + ~FImageTexture() { - mImage = img; + if (mImage) delete mImage; } + void Create8BitPixels(uint8_t* buffer) override; FImageSource *GetImage() const override { return mImage; } FBitmap GetBgraBitmap(const PalEntry *p, int *trans) override; diff --git a/source/platform/win32/i_main.cpp b/source/platform/win32/i_main.cpp index aeb1dcf1c..4cd56e232 100644 --- a/source/platform/win32/i_main.cpp +++ b/source/platform/win32/i_main.cpp @@ -1173,7 +1173,7 @@ int WINAPI wWinMain (HINSTANCE hInstance, HINSTANCE nothing, LPWSTR cmdline, int // Use this to break at a specific allocation number. -// _crtBreakAlloc = 257481; + _crtBreakAlloc = 253018; #endif int ret = DoMain (hInstance); diff --git a/source/platform/win32/st_start.cpp b/source/platform/win32/st_start.cpp index ed20ea4ff..dff57fdcc 100644 --- a/source/platform/win32/st_start.cpp +++ b/source/platform/win32/st_start.cpp @@ -123,7 +123,7 @@ FStartupScreen *FStartupScreen::CreateInstance(int max_progress) FBasicStartupScreen::FBasicStartupScreen(int max_progress, bool show_bar) : FStartupScreen(max_progress) { - if (show_bar) + if (false)//show_bar) { ProgressBar = CreateWindowEx(0, PROGRESS_CLASS, NULL, WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS,