TLuaClass / LuaRegisterObject memory usage?

BlitzMax Forums/BlitzMax Programming/TLuaClass / LuaRegisterObject memory usage?

wickworks(Posted 2014) [#1]
Hi all -
I've been having some inconsistent problems with loading + running lua scripts. It seems to get hung up on random creations of TLuaObjects or function calls (which work fine half the time). I'm using lua scripts pretty heavily so I thought it might be some kind of memory management problem, so I put together the following test code:

main.bmx :
Strict

'reads a script file and returns it as a string
Import "luaLoad.bmx"

Type Printer
	Method say(a$)
		Print a
	EndMethod
EndType

Try
	Local scriptSource$ = getSource("script.lua")

	For Local i = 0 To 100

		Local scriptClass:TLuaClass = TLuaClass.Create(scriptSource)
		Local script:TLuaObject = TLuaObject.Create(scriptClass, Null)

		Local p:Printer = New Printer
		LuaRegisterObject p, "printer"
		
		script.Invoke("init", Null)
	Next

Catch error$
	Print "caught exception "+error$
EndTry


script.lua :
function init()
	printer.say("hello world")
end


This is the output, it gives me the same errors in the same places each run :


It'll run fine if I move either the TLuaObject creation OR the LuaRegisterObject outside of the For loop.

My question :
- Is it a memory thing? I've read this post a couple times but don't completely understand it.

- Is there some way I can clear registered objects or created TLuaObjects after I'm done calling the script so this isn't an issue?

Thanks.


wickworks(Posted 2014) [#2]
Oh, whoops. Here's luaLoad.bmx:
Strict

'reads a .lua file and returns the whole damn thing as a single string
Function getSource$(_filename$)
	Local source$

	Local sourceFile:TStream = ReadFile(_filename)
	While Not Eof(sourceFile)
	
		'get each line of code
		Local line$ = ReadLine(sourceFile)
		
		'omit comments
		Local comment = line.Find("--")
		If comment <> -1 Then line = line[..comment]
		
		'add it to the growing source string (with a fake line break at the end)
		source = source + line + "~n"
		
	Wend
	CloseFile sourceFile
	
	Return source
EndFunction



dan_upright(Posted 2014) [#3]
I don't know if you still need this mate but I added a way to cleanup after maxlua, see this thread: http://www.blitzbasic.co.nz/Community/posts.php?topic=102811


dan_upright(Posted 2014) [#4]
I tweaked your code so it doesn't throw anymore errors for me:
Strict

'reads a script file and returns it as a string
'Import "luaLoad.bmx"

Type Printer
	Method say(a$)
		Print a
	EndMethod
EndType

Try
	Local scriptSource:String = "function init()~nprinter.say('hello world')~nend"

	For Local i = 0 To 100

		Local scriptClass:TLuaClass = TLuaClass.Create(scriptSource)
		Local script:TLuaObject = TLuaObject.Create(scriptClass, Null)

		Local p:Printer = New Printer
		LuaRegisterObject p, "printer"
		
		script.Invoke("init", Null)
		script = Null
		scriptClass = Null
		GCCollect()
		LuaDeregisterObject("printer")
	Next

Catch error$
	Print "caught exception "+error$
EndTry

I should probably have come up with a better way to clean up after TLuaClass and TLuaObject really.


wickworks(Posted 2014) [#5]
Ah, thanks for the reply. Much more simple. Looks to me like it's the GCCollect() that cleans up the errors -- when I comment it out they come back but commenting out the nulling stuff doesn't seem to affect it.

I'm getting an internal error on the maxlua forum link...?


Derron(Posted 2014) [#6]
@internal error
replace "blitzbasic.com" with "blitzmax.com" or vice versa --- it depends on which url you logged in (cookie authentification).


bye
Ron


Brucey(Posted 2014) [#7]
replace "blitzbasic.com" with "blitzmax.com" or vice versa

If you want to post a link to the forum, on the forum, you can simply use the bit starting with "/"... like : {a /Community/posts.php?topic=102699}link{/a}
(replacing curlies with square brackets)

This would get you This page, regardless which base URL you were using.


dan_upright(Posted 2014) [#8]
Looks to me like it's the GCCollect() that cleans up the errors
It is, MaxLua already has cleanup code for TLuaObject and TLuaClass in their Delete() functions, which aren't called until they're garbage collected. The only way to guarantee that happens before the next iteration of the loop is to set them to null and call GCCollect().

Here's a working link to the thread I posted earlier.


wickworks(Posted 2014) [#9]
Man, that's exactly what I was looking for. Thanks!