Skip to content

Commit

Permalink
Fixed memory leak in saving system
Browse files Browse the repository at this point in the history
  • Loading branch information
Amethyst-szs committed Aug 31, 2023
1 parent a134a6c commit d41bc7f
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 29 deletions.
1 change: 0 additions & 1 deletion src/helpers/fsHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

namespace FsHelper {
nn::Result writeFileToPath(void *buf, size_t size, const char *path) {
Logger::log("FsHelper Path:%s\n", path);
nn::fs::FileHandle handle;

if (isFileExist(path)) {
Expand Down
54 changes: 29 additions & 25 deletions src/program/devgui/savedata/DevGuiSaveData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,77 +134,81 @@ nn::Result DevGuiSaveData::write()
{
mWriteStream->rewind();

al::ByamlWriter file = al::ByamlWriter(mHeap, false);
sead::Heap* writerHeap = sead::ExpHeap::create(1500000, "GDWriterHeap", mHeap, 8,
sead::Heap::HeapDirection::cHeapDirection_Forward, false);

al::ByamlWriter* file = new (writerHeap) al::ByamlWriter(writerHeap, false);

file.pushHash();
file->pushHash();

// General information
file.addString("Version", GIT_VER);
file.addString("Theme", mParent->getTheme()->getThemeName());
file.addFloat("Opacity", ImGui::GetStyle().Alpha);
file.addFloat("DockSize", *mParent->getScreenSizeMultiDocked());
file.addFloat("HandSize", *mParent->getScreenSizeMultiHandheld());
file.addBool("UpdateShh", UpdateHandler::instance()->isUpdateSilenced());
file.addInt("MaxGhosts", *GhostManager::instance()->getMaxGhosts());
file->addString("Version", GIT_VER);
file->addString("Theme", mParent->getTheme()->getThemeName());
file->addFloat("Opacity", ImGui::GetStyle().Alpha);
file->addFloat("DockSize", *mParent->getScreenSizeMultiDocked());
file->addFloat("HandSize", *mParent->getScreenSizeMultiHandheld());
file->addBool("UpdateShh", UpdateHandler::instance()->isUpdateSilenced());
file->addInt("MaxGhosts", *GhostManager::instance()->getMaxGhosts());

// Open/close state of all windows
file.pushHash("ActiveWins");
file->pushHash("ActiveWins");

for(int i = 0; i < mParent->getWindowCount(); i++) {
file.addBool(mParent->getWindowNameAtIdx(i), *mParent->getWindowActiveStateAtIdx(i));
file->addBool(mParent->getWindowNameAtIdx(i), *mParent->getWindowActiveStateAtIdx(i));
}

file.pop();
file->pop();

// Current settings in the setting menu
DevGuiSettings* set = mParent->getSettings();
file.pushHash("Settings");
file->pushHash("Settings");

for(int i = 0; i < set->getTotalSettings(); i++) {
sead::FormatFixedSafeString<0x5> idxName("%X", i);
file.addBool(idxName.cstr(), set->getStateByIdx(i));
file->addBool(idxName.cstr(), set->getStateByIdx(i));
}

file.pop();
file->pop();

// Current primitive menu settings
PrimMenuSettings* primSet = mParent->getPrimitiveSettings();
file.pushHash("PrimSet");
file->pushHash("PrimSet");

for(int i = 0; i < primSet->getTotalSettings(); i++) {
sead::FormatFixedSafeString<0x5> idxName("%X", i);
file.addBool(idxName.cstr(), primSet->getSettingEntry(i)->isTrue());
file->addBool(idxName.cstr(), primSet->getSettingEntry(i)->isTrue());
}

file.pop();
file->pop();

// Write the Actor Browser's favorites into array

file.pushHash("FavActorBrowser");
file->pushHash("FavActorBrowser");

for(int i = 0; i < MAXFAVS; i++) {
sead::FixedSafeString<0x40> favName = getActorBrowserFavoriteAtIdx(i);
if(favName.isEmpty())
continue;

sead::FormatFixedSafeString<0x5> idxName("%X", i);
file.addString(idxName.cstr(), favName.cstr());
file->addString(idxName.cstr(), favName.cstr());
}

file.pop();
file->pop();

// Close inital hash and write data
file.pop();
file.write(mWriteStream);
file->pop();
file->write(mWriteStream);

uint size = file.calcPackSize();
uint size = file->calcPackSize();
nn::Result result = FsHelper::writeFileToPath(mWorkBuf, size, SAVEPATH);

Logger::log("Saved data to %s\n", SAVEPATH);

if(static_cast<float>(size) / static_cast<float>(mWorkBufSize) > 0.8f)
Logger::log("\n\n ! WARNING !\n The save file is close to the work buffer limit\n Consider increasing buffer size!\n\n");


writerHeap->destroy();
return result;
}

Expand Down
2 changes: 1 addition & 1 deletion src/program/devgui/savedata/DevGuiSaveData.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,6 @@ class DevGuiSaveData {

sead::RamStreamSrc* mRamStream = nullptr;
DevGuiWriteStream* mWriteStream = nullptr;
static const uint mWorkBufSize = 0x1000;
static const uint mWorkBufSize = 0x1500;
u8 mWorkBuf[mWorkBufSize] = {}; // IMPORTANT - If you are writing a much larger amount of data, may need to expand work buffer size
};
4 changes: 2 additions & 2 deletions src/program/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,9 +232,9 @@ HOOK_DEFINE_TRAMPOLINE(GameSystemInit) {
sead::DebugFontMgrJis1Nvn::instance()->initialize(curHeap, DBG_SHADER_PATH, DBG_FONT_PATH, DBG_TBL_PATH, 0x100000);
}

// creates heap for LunaKit at 7MB directly off the Stationed heap
// creates heap for LunaKit at 9MB directly off the Stationed heap
sead::Heap* lkHeap =
sead::ExpHeap::create(7000000, "LunaKitHeap", al::getStationedHeap(), 8, sead::Heap::HeapDirection::cHeapDirection_Forward, false);
sead::ExpHeap::create(9000000, "LunaKitHeap", al::getStationedHeap(), 8, sead::Heap::HeapDirection::cHeapDirection_Forward, false);
lkHeap->enableLock(true);

sead::Heap* updaterHeap = sead::ExpHeap::create(2500000, "UpdateHeap", lkHeap, 8, sead::Heap::HeapDirection::cHeapDirection_Forward, false);
Expand Down

0 comments on commit d41bc7f

Please sign in to comment.