From 6bc7e1200c5f0cba0d0d5b78a3976f877a692ba1 Mon Sep 17 00:00:00 2001 From: MikalaiR Date: Thu, 28 Jan 2016 16:23:52 +0000 Subject: MirLua: fix memleak in TerminateThread git-svn-id: http://svn.miranda-ng.org/main/trunk@16182 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- plugins/MirLua/src/m_core.cpp | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) (limited to 'plugins/MirLua/src/m_core.cpp') diff --git a/plugins/MirLua/src/m_core.cpp b/plugins/MirLua/src/m_core.cpp index ac58662508..a2bb5a820e 100644 --- a/plugins/MirLua/src/m_core.cpp +++ b/plugins/MirLua/src/m_core.cpp @@ -255,43 +255,59 @@ static int core_GetFullPath(lua_State *L) return 1; } - struct core_ForkThreadParam { lua_State *L; int threadRef; int functionRef; + HANDLE hThread; }; +std::map lstThreads; + +void DestroyThread(core_ForkThreadParam *ftp) +{ + luaL_unref(ftp->L, LUA_REGISTRYINDEX, ftp->functionRef); + luaL_unref(ftp->L, LUA_REGISTRYINDEX, ftp->threadRef); + lstThreads.erase(ftp->hThread); + + delete ftp; +} + void __cdecl ThreadFunc(void *p) { core_ForkThreadParam *ftp = (core_ForkThreadParam*)p; lua_rawgeti(ftp->L, LUA_REGISTRYINDEX, ftp->functionRef); luaM_pcall(ftp->L, 0, 1); - - luaL_unref(ftp->L, LUA_REGISTRYINDEX, ftp->functionRef); - luaL_unref(ftp->L, LUA_REGISTRYINDEX, ftp->threadRef); - delete ftp; + DestroyThread(ftp); } static int core_ForkThread(lua_State *L) { core_ForkThreadParam *p = new core_ForkThreadParam(); + p->L = lua_newthread(L); p->threadRef = luaL_ref(L, LUA_REGISTRYINDEX); lua_pushvalue(L, 1); p->functionRef = luaL_ref(L, LUA_REGISTRYINDEX); - HANDLE hThread = mir_forkthread(ThreadFunc, p); - lua_pushnumber(L, (intptr_t)hThread); + + p->hThread = mir_forkthread(ThreadFunc, p); + lstThreads[p->hThread] = p; + lua_pushnumber(L, (intptr_t)p->hThread); return 1; } static int core_TerminateThread(lua_State *L) { HANDLE hThread = (HANDLE)(intptr_t)luaL_checknumber(L, 1); - BOOL res = TerminateThread(hThread, 0); - lua_pushboolean(L, res); + auto it = lstThreads.find(hThread); + if (it != lstThreads.end()) + { + DestroyThread(it->second); + lua_pushboolean(L, TerminateThread(hThread, 0)); + } + else lua_pushboolean(L, 0); return 1; } -- cgit v1.2.3