From 6fd71f5b277929193db41f84e003867ae52f8d50 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Thu, 6 Dec 2012 22:27:52 -0800 Subject: [PATCH] Fix failed free issues with thread stacks. --- Core/HLE/sceKernelThread.cpp | 50 +++++++++++++++++++++--------------- 1 file changed, 29 insertions(+), 21 deletions(-) diff --git a/Core/HLE/sceKernelThread.cpp b/Core/HLE/sceKernelThread.cpp index 823e9330aa..97400b75ae 100644 --- a/Core/HLE/sceKernelThread.cpp +++ b/Core/HLE/sceKernelThread.cpp @@ -177,7 +177,7 @@ public: (nt.status & THREADSTATUS_READY) ? "READY" : "", (nt.status & THREADSTATUS_WAIT) ? "WAIT" : "", (nt.status & THREADSTATUS_SUSPEND) ? "SUSPEND" : "", - (nt.status & THREADSTATUS_DORMANT) ? "DORMANT" : "", + (nt.status & THREADSTATUS_DORMANT) ? "DORMANT" : "", (nt.status & THREADSTATUS_DEAD) ? "DEAD" : "", nt.waitType, nt.waitID, @@ -190,29 +190,33 @@ public: bool AllocateStack(u32 &stackSize) { - if (nt.attr & PSP_THREAD_ATTR_KERNEL) - { - // Allocate stacks for kernel threads (idle) in kernel RAM - stackBlock = kernelMemory.Alloc(stackSize, true, (std::string("stack/") + nt.name).c_str()); - } - else - { - stackBlock = userMemory.Alloc(stackSize, true, (std::string("stack/") + nt.name).c_str()); - } - if (stackBlock == (u32)-1) - { - ERROR_LOG(HLE, "Failed to allocate stack for thread"); - return false; - } - // Fill the stack. - Memory::Memset(stackBlock, 0xFF, stackSize); + FreeStack(); + + if (nt.attr & PSP_THREAD_ATTR_KERNEL) + { + // Allocate stacks for kernel threads (idle) in kernel RAM + stackBlock = kernelMemory.Alloc(stackSize, true, (std::string("stack/") + nt.name).c_str()); + } + else + { + stackBlock = userMemory.Alloc(stackSize, true, (std::string("stack/") + nt.name).c_str()); + } + if (stackBlock == (u32)-1 || stackBlock == 0) + { + stackBlock = 0; + ERROR_LOG(HLE, "Failed to allocate stack for thread"); + return false; + } + + // Fill the stack. + Memory::Memset(stackBlock, 0xFF, stackSize); context.r[MIPS_REG_SP] = stackBlock + stackSize; nt.initialStack = context.r[MIPS_REG_SP]; - nt.stackSize = stackSize; - // What's this 512? + nt.stackSize = stackSize; + // What's this 512? context.r[MIPS_REG_K0] = context.r[MIPS_REG_SP] - 512; context.r[MIPS_REG_SP] -= 512; - return true; + return true; } void FreeStack() { @@ -227,6 +231,10 @@ public: } } + Thread() : stackBlock(0) + { + } + ~Thread() { FreeStack(); @@ -1078,7 +1086,7 @@ void sceKernelExitDeleteThread() currentThread->nt.exitStatus = PARAM(0); __KernelFireThreadEnd(currentThread); //userMemory.Free(currentThread->stackBlock); - currentThread->stackBlock = -1; + currentThread->stackBlock = 0; __KernelRemoveFromThreadQueue(t); currentThread = 0;