diff options
author | MikalaiR <nikolay.romanovich@narod.ru> | 2016-01-28 16:23:52 +0000 |
---|---|---|
committer | MikalaiR <nikolay.romanovich@narod.ru> | 2016-01-28 16:23:52 +0000 |
commit | 6bc7e1200c5f0cba0d0d5b78a3976f877a692ba1 (patch) | |
tree | 295c6f24206f62ed6a72eeabaf395cdf1318e6aa /plugins/MirLua | |
parent | bb5aff746c50564ded61159c5ae28e6218d0a49d (diff) |
MirLua: fix memleak in TerminateThread
git-svn-id: http://svn.miranda-ng.org/main/trunk@16182 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c
Diffstat (limited to 'plugins/MirLua')
-rw-r--r-- | plugins/MirLua/src/m_core.cpp | 34 |
1 files changed, 25 insertions, 9 deletions
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<HANDLE, core_ForkThreadParam*> 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;
}
|