C++ luabind::对象取消引用问题(简化)
使用C++,lua5.1,luabind 0.7 Lua代码:C++ luabind::对象取消引用问题(简化),c++,reference,garbage-collection,lua,luabind,C++,Reference,Garbage Collection,Lua,Luabind,使用C++,lua5.1,luabind 0.7 Lua代码: -- allocates near 8Mb of memory function fff() local t = {} for i = 1, 300000 do table.insert(t, i) end return t end { luaL_dostring(lua_state, "return fff()"); luabind::object obj(luab
-- allocates near 8Mb of memory
function fff()
local t = {}
for i = 1, 300000 do
table.insert(t, i)
end
return t
end
{
luaL_dostring(lua_state, "return fff()");
luabind::object obj(luabind::from_stack(ls, -1));
}
lua_gc(l_, LUA_GCCOLLECT, 0); // collect garbage
C++代码:
-- allocates near 8Mb of memory
function fff()
local t = {}
for i = 1, 300000 do
table.insert(t, i)
end
return t
end
{
luaL_dostring(lua_state, "return fff()");
luabind::object obj(luabind::from_stack(ls, -1));
}
lua_gc(l_, LUA_GCCOLLECT, 0); // collect garbage
结果:Lua仍有8Mb的分配内存。垃圾回收忽略该表对象。它有参考文献吗?但是在哪里呢?该表仅在程序退出时解除分配(当调用“lua_close”函数时)。如何解决这个问题
谢谢。如果您使用的代码与发布的代码完全相同,我认为Lua堆栈中仍然有一个引用。尝试在luabind::对象创建和lua\u gc调用之间插入lua\u pop(l,1)
另一方面,luabind当前的稳定释放为0.8.1,也有0.9-rc;如果您使用的是最新版本(无论是在这里还是从luabind用户组),您可能会得到更多答案。谢谢。这是这个问题的解决方案,但现在我有另一个相对类似的解决方案-