Question

Say I have a script like

my_global = my_cpp_class()
my_global = nil

now while this properly calls the destructor of my_cpp_class, this code does not:

my_global = my_cpp_class()
call_script("a.lua") -- a.lua contains "my_global = nil"
-- "call_script" is actually a simplified notation 
-- I create script as userdata and then I can 
-- set its child scripts that should be reloaded once script is modified

The same when I simply replace call_script with dofile.

Basically, I want a particular script file to be able to assign "nil" to an existing global so the old value becomes unreachable.

Now what I need this for is script reloading in real time - whenever a script file is modified it is instantly compiled and called with call_script again with its child scripts in runtime, so it should cause globals to be reassigned and hence old values should be garbage collected.

Is there easier, more preferred way to do such script reloads?

Should I use locals somehow, or manipulate with environments? I use luabind, if it's relevant.

Was it helpful?

Solution

If you execute the script within the same lua_State, the global actually is reassigned (you can check that the variable is visible before assigning nil). The C++ object's destructor is run when the object is collected, which may not happen immediately after it becomes unreachable. Calling lua_gc() from C++ or collectgarbage() from Lua will probably help.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top