diff options
| author | coco875 <59367621+coco875@users.noreply.github.com> | 2025-05-23 22:49:06 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-05-23 16:49:06 -0600 |
| commit | 9363e3d77631b1028a765c075a00293bd20c9ba7 (patch) | |
| tree | ab10b69b18b3ecd2eab6d871340d296d0957f6d0 | |
| parent | af4535c3c578425a456c8c34c42f10108f94fd1c (diff) | |
fix memory leaks, avoid invalidate texture (#207)
* Fixed macos
* More stupid fixes
* update with main and update torch and lus and enable action on this branch
* Update FrameInterpolation.h
* Update FrameInterpolation.cpp
* fix some memory leak
* Update torch
* Update torch
* update torch and lus
* reduce texture import
* don't use fork of torch and lus
* Update torch
* Update torch
---------
Co-authored-by: Lywx <kiritodev01@gmail.com>
| -rw-r--r-- | .github/workflows/main.yml | 2 | ||||
| m--------- | libultraship | 0 | ||||
| -rw-r--r-- | src/code_800AF9B0.c | 8 | ||||
| -rw-r--r-- | src/engine/World.cpp | 3 | ||||
| -rw-r--r-- | src/engine/World.h | 1 | ||||
| -rw-r--r-- | src/engine/editor/Editor.cpp | 19 | ||||
| -rw-r--r-- | src/engine/editor/Editor.h | 1 | ||||
| -rw-r--r-- | src/engine/editor/EditorMath.cpp | 4 | ||||
| -rw-r--r-- | src/port/Engine.cpp | 7 | ||||
| -rw-r--r-- | src/port/Game.cpp | 33 | ||||
| -rw-r--r-- | src/port/SpaghettiGui.cpp | 2 | ||||
| -rw-r--r-- | src/port/interpolation/FrameInterpolation.cpp | 5 | ||||
| -rw-r--r-- | src/port/interpolation/FrameInterpolation.h | 2 | ||||
| -rw-r--r-- | src/port/resource/importers/AudioBankFactory.cpp | 1 | ||||
| -rw-r--r-- | src/port/resource/type/AudioBank.cpp | 17 | ||||
| -rw-r--r-- | src/port/resource/type/AudioBank.h | 1 | ||||
| -rw-r--r-- | src/port/resource/type/AudioSample.cpp | 14 | ||||
| -rw-r--r-- | src/port/resource/type/AudioSample.h | 1 | ||||
| -rw-r--r-- | src/render_player.c | 34 | ||||
| m--------- | torch | 0 |
20 files changed, 128 insertions, 27 deletions
diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 496346cb8..6bf2a4a5b 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -2,7 +2,7 @@ name: GenerateBuilds on: push: - branches: ["main"] + branches: ["*"] concurrency: group: ${{ github.workflow }}-${{ github.ref }} diff --git a/libultraship b/libultraship -Subproject d4a22ea7f78eb275b462ce542815d8368849d52 +Subproject 45c4f8d6c19c6176f5e0918917c655ea09ecc21 diff --git a/src/code_800AF9B0.c b/src/code_800AF9B0.c index 732a6e8e9..9876a40cb 100644 --- a/src/code_800AF9B0.c +++ b/src/code_800AF9B0.c @@ -34,15 +34,15 @@ Light D_800E8688 = { { s16 D_8018EDB0; s16 D_8018EDB2; s16 D_8018EDB4; -Vtx* D_8018EDB8; -Vtx* D_8018EDBC; +Vtx D_8018EDB8[480]; +Vtx D_8018EDBC[480]; /*** utils **/ #define SQ(x) ((x) * (x)) void func_800AF9B0(void) { - D_8018EDB8 = (void*) calloc(480, sizeof(Vtx)); - D_8018EDBC = (void*) calloc(480, sizeof(Vtx)); + // D_8018EDB8 = (void*) calloc(480, sizeof(Vtx)); + // D_8018EDBC = (void*) calloc(480, sizeof(Vtx)); } // could be a normal vertex, not a color... diff --git a/src/engine/World.cpp b/src/engine/World.cpp index 8137b3801..6620a2815 100644 --- a/src/engine/World.cpp +++ b/src/engine/World.cpp @@ -22,6 +22,9 @@ extern "C" { } World::World() {} +World::~World() { + CM_CleanWorld(); +} Course* CurrentCourse; Cup* CurrentCup; diff --git a/src/engine/World.h b/src/engine/World.h index bc6845aae..91287b162 100644 --- a/src/engine/World.h +++ b/src/engine/World.h @@ -52,6 +52,7 @@ class World { public: explicit World(); + ~World(); void AddCourse(Course* course); diff --git a/src/engine/editor/Editor.cpp b/src/engine/editor/Editor.cpp index 446da7487..23a947a09 100644 --- a/src/engine/editor/Editor.cpp +++ b/src/engine/editor/Editor.cpp @@ -30,6 +30,11 @@ namespace Editor { Editor::Editor() { } + Editor::~Editor() { + ClearObjects(); + ClearMatrixPool(); + } + void Editor::Load() { printf("Editor: Loading Editor...\n"); eObjectPicker.Load(); @@ -58,10 +63,16 @@ namespace Editor { Ship::Coords mousePos = wnd->GetMousePos(); bool isMouseDown = wnd->GetMouseState(Ship::LUS_MOUSE_BTN_LEFT); - eGameObjects.erase( - std::remove_if(eGameObjects.begin(), eGameObjects.end(), - [](const auto& object) { return (*object->DespawnFlag) == object->DespawnValue; }), - eGameObjects.end()); + auto it = std::remove_if(eGameObjects.begin(), eGameObjects.end(), + [](auto& object) { + if (*object->DespawnFlag == object->DespawnValue) { + delete object; // Free the pointed-to memory + return true; // Remove the pointer from the vector + } + return false; + }); + + eGameObjects.erase(it, eGameObjects.end()); if (isMouseDown && !wasMouseDown) { // Mouse just pressed (Pressed state) diff --git a/src/engine/editor/Editor.h b/src/engine/editor/Editor.h index 759c59355..60e578b21 100644 --- a/src/engine/editor/Editor.h +++ b/src/engine/editor/Editor.h @@ -14,6 +14,7 @@ namespace Editor { class Editor { public: Editor(); + ~Editor(); ObjectPicker eObjectPicker; std::vector<GameObject*> eGameObjects; diff --git a/src/engine/editor/EditorMath.cpp b/src/engine/editor/EditorMath.cpp index 04a299563..f3833008b 100644 --- a/src/engine/editor/EditorMath.cpp +++ b/src/engine/editor/EditorMath.cpp @@ -352,8 +352,8 @@ bool IntersectRaySphere(const Ray& ray, const FVector& sphereCenter, float radiu // Transform a matrix to a matrix identity void Editor_MatrixIdentity(Mat4 mtx) { - register s32 i; - register s32 k; + s32 i; + s32 k; for (i = 0; i < 4; i++) { for (k = 0; k < 4; k++) { diff --git a/src/port/Engine.cpp b/src/port/Engine.cpp index 15acab0ac..70ea4054c 100644 --- a/src/port/Engine.cpp +++ b/src/port/Engine.cpp @@ -298,6 +298,9 @@ void GameEngine::Destroy() { #ifdef __SWITCH__ Ship::Switch::Exit(); #endif + GameUI::Destroy(); + delete GameEngine::Instance; + GameEngine::Instance = nullptr; } bool ShouldClearTextureCacheAtEndOfFrame = false; @@ -509,7 +512,9 @@ ImFont* GameEngine::CreateFontWithSize(float size, std::string fontPath) { initData->Path = fontPath; std::shared_ptr<Ship::Font> fontData = std::static_pointer_cast<Ship::Font>( Ship::Context::GetInstance()->GetResourceManager()->LoadResource(fontPath, false, initData)); - font = mImGuiIo->Fonts->AddFontFromMemoryTTF(fontData->Data, fontData->DataSize, size); + char* fontDataPtr = (char*)malloc(fontData->DataSize); + memcpy(fontDataPtr, fontData->Data, fontData->DataSize); + font = mImGuiIo->Fonts->AddFontFromMemoryTTF(fontDataPtr, fontData->DataSize, size); } // FontAwesome fonts need to have their sizes reduced by 2.0f/3.0f in order to align correctly float iconFontSize = size * 2.0f / 3.0f; diff --git a/src/port/Game.cpp b/src/port/Game.cpp index 33b4e9d16..caccd9f90 100644 --- a/src/port/Game.cpp +++ b/src/port/Game.cpp @@ -198,6 +198,38 @@ void CustomEngineInit() { // gModelLoader.Load(); } +void CustomEngineDestroy() { + delete gMarioRaceway; + delete gChocoMountain; + delete gBowsersCastle; + delete gBansheeBoardwalk; + delete gYoshiValley; + delete gFrappeSnowland; + delete gKoopaTroopaBeach; + delete gRoyalRaceway; + delete gLuigiRaceway; + delete gMooMooFarm; + delete gToadsTurnpike; + delete gKalimariDesert; + delete gSherbetLand; + delete gRainbowRoad; + delete gWarioStadium; + delete gBlockFort; + delete gSkyscraper; + delete gDoubleDeck; + delete gDkJungle; + delete gBigDonut; + delete gPodiumCeremony; + delete gHarbour; + delete gTestCourse; + + delete gMushroomCup; + delete gFlowerCup; + delete gStarCup; + delete gSpecialCup; + delete gBattleCup; +} + extern "C" { void HM_InitIntro() { @@ -873,6 +905,7 @@ extern "C" while (WindowIsRunning()) { push_frame(); } + CustomEngineDestroy(); // GameEngine::Instance->ProcessFrame(push_frame); GameEngine::Instance->Destroy(); return 0; diff --git a/src/port/SpaghettiGui.cpp b/src/port/SpaghettiGui.cpp index 09ac63234..6c703456d 100644 --- a/src/port/SpaghettiGui.cpp +++ b/src/port/SpaghettiGui.cpp @@ -12,7 +12,7 @@ #include <SDL_hints.h> #include <SDL_video.h> -#include "graphic/Fast3D/gfx_metal.h" +#include "graphic/Fast3D/backends/gfx_metal.h" #include <imgui_impl_metal.h> #include <imgui_impl_sdl2.h> #else diff --git a/src/port/interpolation/FrameInterpolation.cpp b/src/port/interpolation/FrameInterpolation.cpp index c86f63089..836e6c70c 100644 --- a/src/port/interpolation/FrameInterpolation.cpp +++ b/src/port/interpolation/FrameInterpolation.cpp @@ -52,6 +52,11 @@ static bool invert_matrix(const float m[16], float invOut[16]); using namespace std; +extern "C" { +extern Mat4* gInterpolationMatrix; +void mtxf_translate(Mat4, Vec3f); +} + namespace { enum class Op { diff --git a/src/port/interpolation/FrameInterpolation.h b/src/port/interpolation/FrameInterpolation.h index 06a7cf3d4..c59743681 100644 --- a/src/port/interpolation/FrameInterpolation.h +++ b/src/port/interpolation/FrameInterpolation.h @@ -19,7 +19,7 @@ extern "C" { #define TAG_ITEM_ADDR(x) ((u32) 0x10000000 | (u32)x) #define TAG_SMOKE_DUST(x) ((u32) 0x20000000 | (u32) (x)) #define TAG_LETTER(x) ((u32)0x30000000 | (u32) (x)) -#define TAG_OBJECT(x) ((u32)0x40000000 | (u32)(x)) +#define TAG_OBJECT(x) ((u32)0x40000000 | (u32) (uintptr_t) (x)) void FrameInterpolation_ShouldInterpolateFrame(bool shouldInterpolate); diff --git a/src/port/resource/importers/AudioBankFactory.cpp b/src/port/resource/importers/AudioBankFactory.cpp index ee400c1b5..650767881 100644 --- a/src/port/resource/importers/AudioBankFactory.cpp +++ b/src/port/resource/importers/AudioBankFactory.cpp @@ -21,6 +21,7 @@ SM64::AudioBankFactoryV0::ReadResource(std::shared_ptr<Ship::File> file, auto* instrument = new Instrument(); bool valid = reader->ReadUByte(); if(!valid){ + delete instrument; bank->instruments.push_back(nullptr); continue; } diff --git a/src/port/resource/type/AudioBank.cpp b/src/port/resource/type/AudioBank.cpp index f91b767d2..7cbe3073a 100644 --- a/src/port/resource/type/AudioBank.cpp +++ b/src/port/resource/type/AudioBank.cpp @@ -8,4 +8,21 @@ CtlEntry* AudioBank::GetPointer() { size_t AudioBank::GetPointerSize() { return sizeof(mData); } +AudioBank::~AudioBank() { + for (auto& instrument : instruments) { + if (instrument != nullptr) { + if (instrument->envelope != nullptr) { + delete[] instrument->envelope; + instrument->envelope = nullptr; + } + delete instrument; + } + + } + for (auto& drum : drums) { + delete drum; + } + instruments.clear(); + drums.clear(); +} }
\ No newline at end of file diff --git a/src/port/resource/type/AudioBank.h b/src/port/resource/type/AudioBank.h index a8541fe84..b9beefa78 100644 --- a/src/port/resource/type/AudioBank.h +++ b/src/port/resource/type/AudioBank.h @@ -47,6 +47,7 @@ class AudioBank : public Ship::Resource<CtlEntry> { using Resource::Resource; AudioBank() : Resource(std::shared_ptr<Ship::ResourceInitData>()) {} + ~AudioBank() override; CtlEntry* GetPointer(); size_t GetPointerSize(); diff --git a/src/port/resource/type/AudioSample.cpp b/src/port/resource/type/AudioSample.cpp index cbdbd6573..f9b087af7 100644 --- a/src/port/resource/type/AudioSample.cpp +++ b/src/port/resource/type/AudioSample.cpp @@ -8,4 +8,18 @@ AudioBankSample* AudioSample::GetPointer() { size_t AudioSample::GetPointerSize() { return sizeof(mData); } +AudioSample::~AudioSample() { + if (mData.sampleAddr != nullptr) { + // delete[] mData.sampleAddr; + mData.sampleAddr = nullptr; + } + if (mData.book->book != nullptr) { + delete[] mData.book->book; + mData.book->book = nullptr; + } + if (mData.loop->state != nullptr) { + delete[] mData.loop->state; + mData.loop->state = nullptr; + } +} }
\ No newline at end of file diff --git a/src/port/resource/type/AudioSample.h b/src/port/resource/type/AudioSample.h index 35f1d8d33..2d6f46c96 100644 --- a/src/port/resource/type/AudioSample.h +++ b/src/port/resource/type/AudioSample.h @@ -34,6 +34,7 @@ class AudioSample : public Ship::Resource<AudioBankSample> { using Resource::Resource; AudioSample() : Resource(std::shared_ptr<Ship::ResourceInitData>()) {} + ~AudioSample() override; AudioBankSample* GetPointer(); size_t GetPointerSize(); diff --git a/src/render_player.c b/src/render_player.c index 017b0fd18..ca4247ef9 100644 --- a/src/render_player.c +++ b/src/render_player.c @@ -48,7 +48,6 @@ s16 gMatrixEffectCount; s32 D_80164AF4[3]; struct_D_802F1F80* gPlayerPalette; static const char* sKartUpperTexture; -static const char* sKartLowerTexture; u16 gPlayerRedEffect[8]; u16 gPlayerGreenEffect[8]; u16 gPlayerBlueEffect[8]; @@ -1600,6 +1599,19 @@ void render_player_shadow_credits(Player* player, s8 playerId, s8 arg2) { gSPTexture(gDisplayListHead++, 1, 1, 0, G_TX_RENDERTILE, G_OFF); } +Vtx player_vtx[] = { + { { { 9, 18, -6 }, 0, { 4032, 0 }, { 0xFF, 0xFF, 0xFF, 0xFF } } }, + { { { 9, 0, -6 }, 0, { 4032, 4032 }, { 0xFF, 0xFF, 0xFF, 0xFF } } }, + { { { -9, 18, -6 }, 0, { 0, 0 }, { 0xFF, 0xFF, 0xFF, 0xFF } } }, + { { { -9, 0, -6 }, 0, { 0, 4032 }, { 0xFF, 0xFF, 0xFF, 0xFF } } }, +}; +Vtx player_vtx_flip[] = { + { { { 9, 18, -6 }, 0, { 0, 0 }, { 0xFF, 0xFF, 0xFF, 0xFF } } }, + { { { 9, 0, -6 }, 0, { 0, 4032 }, { 0xFF, 0xFF, 0xFF, 0xFF } } }, + { { { -9, 18, -6 }, 0, { 4032, 0 }, { 0xFF, 0xFF, 0xFF, 0xFF } } }, + { { { -9, 0, -6 }, 0, { 4032, 4032 }, { 0xFF, 0xFF, 0xFF, 0xFF } } }, +}; + void render_kart(Player* player, s8 playerId, s8 screenId, s8 arg3) { UNUSED s32 pad; Mat4 mtx; @@ -1727,19 +1739,15 @@ void render_kart(Player* player, s8 playerId, s8 screenId, s8 arg3) { } // Render heads - gDPLoadTextureBlock(gDisplayListHead++, sKartUpperTexture, G_IM_FMT_CI, G_IM_SIZ_8b, 64, 32, 0, + gDPLoadTextureBlock(gDisplayListHead++, sKartUpperTexture, G_IM_FMT_CI, G_IM_SIZ_8b, 64, 64, 0, G_TX_NOMIRROR | G_TX_CLAMP, G_TX_NOMIRROR | G_TX_CLAMP, G_TX_NOMASK, G_TX_NOMASK, G_TX_NOLOD, G_TX_NOLOD); - gSPVertex(gDisplayListHead++, &D_800DDBB4[playerId][arg3], 4, 0); - gSPDisplayList(gDisplayListHead++, common_square_plain_render); - - // Render karts - u8* test = (u8*) LOAD_ASSET(sKartUpperTexture); - gDPLoadTextureBlock(gDisplayListHead++, test + 0x7C0, G_IM_FMT_CI, G_IM_SIZ_8b, 64, 32, 0, - G_TX_NOMIRROR | G_TX_CLAMP, G_TX_NOMIRROR | G_TX_CLAMP, G_TX_NOMASK, G_TX_NOMASK, G_TX_NOLOD, - G_TX_NOLOD); - gSPVertex(gDisplayListHead++, &D_800DDBB4[playerId][arg3 + 4], 4, 0); - gSPDisplayList(gDisplayListHead++, common_square_plain_render); + if (arg3 == 0) { + gSPVertex(gDisplayListHead++, player_vtx, 4, 0); + } else { + gSPVertex(gDisplayListHead++, player_vtx_flip, 4, 0); + } + gSP2Triangles(gDisplayListHead++, 0, 1, 2, 0, 1, 3, 2, 0); gSPTexture(gDisplayListHead++, 1, 1, 0, G_TX_RENDERTILE, G_OFF); gDPSetAlphaCompare(gDisplayListHead++, G_AC_NONE); @@ -1964,7 +1972,7 @@ void render_player(Player* player, s8 playerId, s8 screenId) { func_80025DE8(player, playerId, screenId, var_v1); } // Allows wheels to spin - gSPInvalidateTexCache(gDisplayListHead++, sKartLowerTexture); + gSPInvalidateTexCache(gDisplayListHead++, sKartUpperTexture); } void func_80026A48(Player* player, s8 arg1) { diff --git a/torch b/torch -Subproject 8e56ea0294973b7c9f8f077a131b40fce13b848 +Subproject f75facb20883570ed091e8ae733ec0539f606e5 |
