memset the memory to 0 before freeing

This commit is contained in:
Maschell 2023-03-26 15:30:15 +02:00
parent 54d0f80426
commit fa3292fa28
1 changed files with 7 additions and 1 deletions

View File

@ -28,10 +28,11 @@ public:
typedef void (*Callback)(CThread *thread, void *arg); typedef void (*Callback)(CThread *thread, void *arg);
//! constructor //! constructor
explicit CThread(int32_t iAttr, int32_t iPriority = 16, int32_t iStackSize = 0x8000, CThread::Callback callback = nullptr, void *callbackArg = nullptr) explicit CThread(int32_t iAttr, int32_t iPriority = 16, int32_t stacksize = 0x8000, CThread::Callback callback = nullptr, void *callbackArg = nullptr)
: pThread(nullptr), pThreadStack(nullptr), pCallback(callback), pCallbackArg(callbackArg) { : pThread(nullptr), pThreadStack(nullptr), pCallback(callback), pCallbackArg(callbackArg) {
//! save attribute assignment //! save attribute assignment
iAttributes = iAttr; iAttributes = iAttr;
iStackSize = stacksize;
//! allocate the thread on the default Cafe OS heap //! allocate the thread on the default Cafe OS heap
pThread = (OSThread *) gMEMAllocFromDefaultHeapExForThreads(sizeof(OSThread), 0x10); pThread = (OSThread *) gMEMAllocFromDefaultHeapExForThreads(sizeof(OSThread), 0x10);
//! allocate the stack on the default Cafe OS heap //! allocate the stack on the default Cafe OS heap
@ -123,11 +124,15 @@ public:
} }
OSJoinThread(pThread, nullptr); OSJoinThread(pThread, nullptr);
} }
// Some games (e.g. Minecraft) expect the default heap to be empty.
// Make sure to clean up the memory after using it
//! free the thread stack buffer //! free the thread stack buffer
if (pThreadStack) { if (pThreadStack) {
memset(pThreadStack, 0, iStackSize);
gMEMFreeToDefaultHeapForThreads(pThreadStack); gMEMFreeToDefaultHeapForThreads(pThreadStack);
} }
if (pThread) { if (pThread) {
memset(pThread, 0, sizeof(OSThread));
gMEMFreeToDefaultHeapForThreads(pThread); gMEMFreeToDefaultHeapForThreads(pThread);
} }
pThread = nullptr; pThread = nullptr;
@ -151,6 +156,7 @@ private:
return 0; return 0;
} }
uint32_t iStackSize;
int32_t iAttributes; int32_t iAttributes;
OSThread *pThread; OSThread *pThread;
uint8_t *pThreadStack; uint8_t *pThreadStack;