mirror of
https://github.com/wiiu-env/PayloadLoaderPayload.git
synced 2024-12-12 19:12:36 +01:00
Another attempt of fixing the memory leak
This commit is contained in:
parent
d866a17af8
commit
92ef9a681e
@ -57,6 +57,7 @@ IMPORT(OSCompareAndSwapAtomicEx);
|
|||||||
IMPORT(OSCompareAndSwapAtomic);
|
IMPORT(OSCompareAndSwapAtomic);
|
||||||
IMPORT(OSGetThreadSpecific);
|
IMPORT(OSGetThreadSpecific);
|
||||||
IMPORT(OSSetThreadSpecific);
|
IMPORT(OSSetThreadSpecific);
|
||||||
|
IMPORT(OSReport);
|
||||||
|
|
||||||
IMPORT(exit);
|
IMPORT(exit);
|
||||||
IMPORT(_Exit);
|
IMPORT(_Exit);
|
||||||
|
39
src/main.cpp
39
src/main.cpp
@ -1,5 +1,5 @@
|
|||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* Copyright (C) 2018-2020 Maschell
|
* Copyright (C) 2018-2022 Maschell
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* This program is free software: you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU General Public License as published by
|
* it under the terms of the GNU General Public License as published by
|
||||||
@ -15,6 +15,7 @@
|
|||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
|
#include <coreinit/debug.h>
|
||||||
#include <coreinit/dynload.h>
|
#include <coreinit/dynload.h>
|
||||||
#include <coreinit/memexpheap.h>
|
#include <coreinit/memexpheap.h>
|
||||||
#include <coreinit/screen.h>
|
#include <coreinit/screen.h>
|
||||||
@ -43,13 +44,16 @@ std::string PayloadSelectionScreen(const std::map<std::string, std::string> &pay
|
|||||||
|
|
||||||
extern "C" void __init_wut();
|
extern "C" void __init_wut();
|
||||||
extern "C" void __fini_wut();
|
extern "C" void __fini_wut();
|
||||||
uint32_t memory_start = 0;
|
MEMExpHeapBlock *memory_start = nullptr;
|
||||||
|
|
||||||
extern "C" uint32_t start_wrapper(int argc, char **argv) {
|
extern "C" uint32_t start_wrapper(int argc, char **argv) {
|
||||||
doKernelSetup();
|
doKernelSetup();
|
||||||
InitFunctionPointers();
|
InitFunctionPointers();
|
||||||
|
|
||||||
memory_start = (uint32_t) malloc(1024);
|
// Save last entry on mem2 heap to detect leaked memory
|
||||||
|
MEMHeapHandle mem2_heap_handle = MEMGetBaseHeapHandle(MEM_BASE_HEAP_MEM2);
|
||||||
|
auto heap = (MEMExpHeap *) mem2_heap_handle;
|
||||||
|
memory_start = heap->usedList.tail;
|
||||||
|
|
||||||
__init_wut();
|
__init_wut();
|
||||||
|
|
||||||
@ -92,17 +96,21 @@ extern "C" uint32_t start_wrapper(int argc, char **argv) {
|
|||||||
extern "C" int _start(int argc, char **argv) {
|
extern "C" int _start(int argc, char **argv) {
|
||||||
uint32_t entryPoint = start_wrapper(argc, argv);
|
uint32_t entryPoint = start_wrapper(argc, argv);
|
||||||
|
|
||||||
// Somewhere in this loader is a memory leak.
|
MEMHeapHandle mem2_heap_handle = MEMGetBaseHeapHandle(MEM_BASE_HEAP_MEM2);
|
||||||
// This is a hacky solution to free that memory.
|
auto heap = (MEMExpHeap *) mem2_heap_handle;
|
||||||
uint32_t head_end = (uint32_t) malloc(1024);
|
// free leaked memory
|
||||||
MEMExpHeapBlock *curUsedBlock = (MEMExpHeapBlock *) (head_end - 0x14);
|
if (memory_start) {
|
||||||
while (curUsedBlock != 0) {
|
int leak_count = 0;
|
||||||
curUsedBlock = curUsedBlock->prev;
|
while (true) {
|
||||||
free(&curUsedBlock[1]);
|
MEMExpHeapBlock *memory_end = heap->usedList.tail;
|
||||||
|
if (memory_end == memory_start) {
|
||||||
if (((uint32_t) &curUsedBlock[1]) == memory_start) {
|
break;
|
||||||
break;
|
}
|
||||||
|
auto mem_ptr = &memory_end[1]; // &memory_end + sizeof(MEMExpHeapBlock);
|
||||||
|
free(mem_ptr);
|
||||||
|
leak_count++;
|
||||||
}
|
}
|
||||||
|
OSReport("Freed %d leaked memory blocks\n", leak_count);
|
||||||
}
|
}
|
||||||
|
|
||||||
int res = -1;
|
int res = -1;
|
||||||
@ -219,7 +227,6 @@ std::string PayloadSelectionScreen(const std::map<std::string, std::string> &pay
|
|||||||
|
|
||||||
int pos = 0;
|
int pos = 0;
|
||||||
|
|
||||||
|
|
||||||
OSScreenPutFontEx(SCREEN_TV, 0, pos, header.c_str());
|
OSScreenPutFontEx(SCREEN_TV, 0, pos, header.c_str());
|
||||||
OSScreenPutFontEx(SCREEN_DRC, 0, pos, header.c_str());
|
OSScreenPutFontEx(SCREEN_DRC, 0, pos, header.c_str());
|
||||||
|
|
||||||
@ -256,6 +263,9 @@ std::string PayloadSelectionScreen(const std::map<std::string, std::string> &pay
|
|||||||
|
|
||||||
OSSleepTicks(OSMillisecondsToTicks(16));
|
OSSleepTicks(OSMillisecondsToTicks(16));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
free(screenBuffer);
|
||||||
|
|
||||||
int i = 0;
|
int i = 0;
|
||||||
for (auto const &[key, val] : payloads) {
|
for (auto const &[key, val] : payloads) {
|
||||||
if (i == selected) {
|
if (i == selected) {
|
||||||
@ -263,6 +273,5 @@ std::string PayloadSelectionScreen(const std::map<std::string, std::string> &pay
|
|||||||
}
|
}
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
free(screenBuffer);
|
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user