Possible Memory Leak due to multiple lua states
Closed this issue · 1 comments
Running the basic test (hello.lua) using valgrind points to a leak in created lua_states.
luaproc/tests$ valgrind --leak-check=full lua hello.lua
==15440== Memcheck, a memory error detector
==15440== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==15440== Using Valgrind-3.10.0.SVN and LibVEX; rerun with -h for copyright info
==15440== Command: lua y.lua
==15440==
hello world from luaproc
==15440==
==15440== HEAP SUMMARY:
==15440== in use at exit: 13,548 bytes in 161 blocks
==15440== total heap usage: 1,095 allocs, 934 frees, 107,076 bytes allocated
==15440==
==15440== 3,337 (704 direct, 2,633 indirect) bytes in 1 blocks are definitely lost in loss record 48 of 50
==15440== at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==15440== by 0x4C2CF1F: realloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==15440== by 0x40ECD1: lua_newstate (in /usr/bin/lua5.2)
==15440== by 0x414F9C: luaL_newstate (in /usr/bin/lua5.2)
==15440== by 0x5F78915: ???
==15440== by 0x5F79D0B: ???
==15440== by 0x4081AC: ??? (in /usr/bin/lua5.2)
==15440== by 0x4084FC: ??? (in /usr/bin/lua5.2)
==15440== by 0x406277: lua_callk (in /usr/bin/lua5.2)
==15440== by 0x41FC7E: ??? (in /usr/bin/lua5.2)
==15440== by 0x4081AC: ??? (in /usr/bin/lua5.2)
==15440== by 0x411FA3: ??? (in /usr/bin/lua5.2)
==15440==
==15440== 3,342 (704 direct, 2,638 indirect) bytes in 1 blocks are definitely lost in loss record 49 of 50
==15440== at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==15440== by 0x4C2CF1F: realloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==15440== by 0x40ECD1: lua_newstate (in /usr/bin/lua5.2)
==15440== by 0x414F9C: luaL_newstate (in /usr/bin/lua5.2)
==15440== by 0x5F78BDA: ???
==15440== by 0x5F78D38: ???
==15440== by 0x4081AC: ??? (in /usr/bin/lua5.2)
==15440== by 0x4084FC: ??? (in /usr/bin/lua5.2)
==15440== by 0x407B48: ??? (in /usr/bin/lua5.2)
==15440== by 0x408740: ??? (in /usr/bin/lua5.2)
==15440== by 0x409826: ??? (in /usr/bin/lua5.2)
==15440== by 0x4098AE: ??? (in /usr/bin/lua5.2)
==15440==
==15440== 3,507 (704 direct, 2,803 indirect) bytes in 1 blocks are definitely lost in loss record 50 of 50
==15440== at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==15440== by 0x4C2CF1F: realloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==15440== by 0x40ECD1: lua_newstate (in /usr/bin/lua5.2)
==15440== by 0x414F9C: luaL_newstate (in /usr/bin/lua5.2)
==15440== by 0x5F79C32: ???
==15440== by 0x4081AC: ??? (in /usr/bin/lua5.2)
==15440== by 0x4084FC: ??? (in /usr/bin/lua5.2)
==15440== by 0x406277: lua_callk (in /usr/bin/lua5.2)
==15440== by 0x41FC7E: ??? (in /usr/bin/lua5.2)
==15440== by 0x4081AC: ??? (in /usr/bin/lua5.2)
==15440== by 0x411FA3: ??? (in /usr/bin/lua5.2)
==15440== by 0x408508: ??? (in /usr/bin/lua5.2)
==15440==
==15440== LEAK SUMMARY:
==15440== definitely lost: 2,112 bytes in 3 blocks
==15440== indirectly lost: 8,074 bytes in 149 blocks
==15440== possibly lost: 0 bytes in 0 blocks
==15440== still reachable: 3,362 bytes in 9 blocks
==15440== suppressed: 0 bytes in 0 blocks
==15440== Reachable blocks (those to which a pointer was found) are not shown.
==15440== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==15440==
==15440== For counts of detected and suppressed errors, rerun with: -v
==15440== ERROR SUMMARY: 3 errors from 3 contexts (suppressed: 1 from 1)
Debugging for a while, I found that processes are being cleaned, however the states created for channels, sched_init and sched_join are not being destroyed.
(LNS => LuaNewState; LC => LuaClose)
LNS-NewChannel - l:749:luaproc.c
LNS-SchedInit - l:186:lpsched.c
LNS-NewProc - l:348:luaproc.c
LNS-SchedJoin - l:293:lpsched.c
LNS-NewProc
LNS-NewProc
hello world from luaproc
LC-Proc - l:241:luaproc.c
LC-Proc
LC-Proc
Adding lua_close in the finalizer function (join_workers).