I have Lua 5.4 embedded in a project and implemented __close metamethod for a userdata, which is a vector of numbers. The project has its own script editor and is able to run the scripts. The implementation of __close is exactly the same as __gc metamethod, which is shown below:
int Vector_gc(lua_State* L)
{
CVector<double>* vector = *(CVector<double>**)lua_touserdata(L, 1);
delete vector;
return 0;
}
1) In a script file, when running the following script, there is no problem at all:
local vector=std.rand(5000) --generate 5000 random numbers
vector=nil
collectgarbage()
2) Similarly, if I run the following script file more than once, again no problem at all:
local vector <close> = std.rand(5000)
3) However, the following script using collectgarbage when run more than once causes crash:
local vector <close> = std.rand(5000)
collectgarbage()
EDIT 1:
4) The following causes an immediate crash.
if(true) then
local vector <close> = std.rand(5000)
vector[2]=1
end
collectgarbage()
Am I misunderstanding/missing something about to-be-closed variables?