From 58a1f0c68eb3f4fd6a743d5dc0652fb8af321b23 Mon Sep 17 00:00:00 2001 From: smea Date: Wed, 29 Oct 2014 21:26:58 -0700 Subject: [PATCH] plugged a bunch of handle leaks ! --- libctru/source/initSystem.c | 5 +++++ libctru/source/services/apt.c | 2 +- libctru/source/services/fs.c | 5 ++++- libctru/source/services/gsp.c | 1 + libctru/source/services/hid.c | 1 + libctru/source/services/irrst.c | 1 + libctru/source/srv.c | 14 +++++++++++++- 7 files changed, 26 insertions(+), 3 deletions(-) diff --git a/libctru/source/initSystem.c b/libctru/source/initSystem.c index 57ef26d..b494567 100644 --- a/libctru/source/initSystem.c +++ b/libctru/source/initSystem.c @@ -22,6 +22,8 @@ extern char* fake_heap_end; static void initArgv(); static u32 heapBase; +void __destroy_handle_list(void); + void __attribute__((noreturn)) __ctru_exit(int rc) { // Run the global destructors @@ -35,6 +37,9 @@ void __attribute__((noreturn)) __ctru_exit(int rc) // Unmap the application heap svcControlMemory(&heapBase, heapBase, 0x0, __heap_size, MEMOP_FREE, 0x0); + // Close some handles + __destroy_handle_list(); + // Jump to the loader if it provided a callback if (__system_retAddr) __system_retAddr(); diff --git a/libctru/source/services/apt.c b/libctru/source/services/apt.c index 8c4c6ec..3b995a8 100644 --- a/libctru/source/services/apt.c +++ b/libctru/source/services/apt.c @@ -360,7 +360,7 @@ void aptExit() svcCloseHandle(aptSleepSync); svcCloseHandle(aptStatusMutex); - //svcCloseHandle(aptLockHandle); + svcCloseHandle(aptLockHandle); svcCloseHandle(aptStatusEvent); } diff --git a/libctru/source/services/fs.c b/libctru/source/services/fs.c index 00a184b..47946f8 100644 --- a/libctru/source/services/fs.c +++ b/libctru/source/services/fs.c @@ -788,7 +788,10 @@ FSFILE_Close(Handle handle) if((ret = svcSendSyncRequest(handle))) return ret; - return cmdbuf[1]; + ret = cmdbuf[1]; + if(!ret)ret = svcCloseHandle(handle); + + return ret; } /*! Read data from an open file diff --git a/libctru/source/services/gsp.c b/libctru/source/services/gsp.c index 24fff1d..514a745 100644 --- a/libctru/source/services/gsp.c +++ b/libctru/source/services/gsp.c @@ -59,6 +59,7 @@ void gspExitEventHandler() // Stop event thread gspRunEvents = false; svcWaitSynchronization(gspEventThread, 1000000000); + svcCloseHandle(gspEventThread); // Free events int i; diff --git a/libctru/source/services/hid.c b/libctru/source/services/hid.c index eb14c5d..cc17b68 100644 --- a/libctru/source/services/hid.c +++ b/libctru/source/services/hid.c @@ -48,6 +48,7 @@ cleanup1: void hidExit() { // Unmap HID sharedmem and close handles. + int i; for(i=0; i<5; i++)svcCloseHandle(hidEvents[i]); svcUnmapMemoryBlock(hidMemHandle, (u32)hidSharedMem); svcCloseHandle(hidMemHandle); svcCloseHandle(hidHandle); diff --git a/libctru/source/services/irrst.c b/libctru/source/services/irrst.c index f00aaa2..87121ac 100644 --- a/libctru/source/services/irrst.c +++ b/libctru/source/services/irrst.c @@ -45,6 +45,7 @@ cleanup1: void irrstExit() { irrstUsed = false; + svcCloseHandle(irrstEvent); // Unmap ir:rst sharedmem and close handles. svcUnmapMemoryBlock(irrstMemHandle, (u32)irrstSharedMem); svcCloseHandle(irrstMemHandle); diff --git a/libctru/source/srv.c b/libctru/source/srv.c index 257b585..ad6e510 100644 --- a/libctru/source/srv.c +++ b/libctru/source/srv.c @@ -58,6 +58,18 @@ Handle __get_handle_from_list(const char* name) { return 0; } +void __destroy_handle_list(void) { + if((u32)__service_ptr == 0) + return; + + u32 i, num = __service_ptr->num; + + for(i=0; iservices[i].handle); + + __service_ptr->num = 0; +} + Result srvInit() { Result rc = 0; @@ -163,4 +175,4 @@ Result srvUnregisterProcess(u32 procid) if((rc = svcSendSyncRequest(g_srv_handle))) return rc; return cmdbuf[1]; -} \ No newline at end of file +}