Memory leak when set a table.var to Lua

BlitzMax Forums/BlitzMax Programming/Memory leak when set a table.var to Lua

MOBii(Posted 2016) [#1]
I have a BIG problem...


From Lua I execute TimerLoop() every 10ms that set a Lua table.variable: SYS.MOBii_0 to SYS.MOBii_999 to its id value
When I start the Application is 18.0 MB in the taskmgr and after 100 iterations my application has grown to 18.5 MB
so after sending 100000 table.variable: SYS.MOBii_XX to the Lua from BlitzMax with: BLLua.MOBii_TableSetInt("SYS", "MOBii_" + a , i)
the Application use 500000 more bytes!

If I use BLLua.MOBii_SetInt("MOBii_" + a , i) the Application still eat memory but little less!

Do I do anything Wrong with MOBii_TableSetInt?

Lua script:
------------------------------------------------[TimerLoop]---
function TimerLoop()
	if a == 100 then echo("value: "..SYS.MOBii_0) a = 0 end
	
	MOBii(SYS.MOBii_0 + 1)
	a = a + 1
end



BlitzMax code:
lua_register(L, "MOBii", Lua_MOBii)		' Register my Test function
...

' -----------------------------------------------------------------------[BLLua.Lua_MOBii]---
Function Lua_MOBii:Int(_L:Byte Ptr)
	If _L Then
 		Local i:Int = BLLua.MOBii_GetInt(1, "MOBii", 1, -1)
		For Local a:Int = 0 To 999
 			BLLua.MOBii_TableSetInt("SYS", "MOBii_" + a , i)
' 			BLLua.MOBii_SetInt("MOBii_" + a , i)
		Next
	End If
	Return 0
End Function

' ---------------------------------------------------------------[BLLua.MOBii_TableSetInt]---
Method MOBii_TableSetInt(_Table:String, _Namn:String, _Data:Int)
	If L Then
		lua_pushstring(L, _Table)								' put the table in varname
		lua_gettable(L, LUA_GLOBALSINDEX)
		lua_pushstring(L, _Namn)								' push the key
		lua_pushinteger(L, _Data)								' 1st argument
		lua_settable(L, -3)									' set table["key"] to "value"
		lua_pop(L, 1)										' If I don't use this pop the application crash after 2 minutes..
	End If
End Method

' --------------------------------------------------------------------[BLLua.MOBii_SetInt]---
Method MOBii_SetInt(_Namn:String, _Data:Int)
	If L Then
		lua_pushinteger(L, _Data)
		lua_setglobal(L, _Namn)
	End If
End Method
After some time the Application use more than 400Mb of memory and keeps growing...


skidracer(Posted 2017) [#2]
Where did your blitzmax code come from?

How does lua_pop work? Should you be calling it once, three times or none at all?


LT(Posted 2017) [#3]
I'm not clear on the question.

Your lua_pop is popping the table from the stack. Without it, you'll end up with 1000 table references on the stack (each iteration) and likely overflow its cache (thus the crash).