我有一個簡單Lua
腳本的Lua/Luabind:由對象構造的對象保持分配
function TestFunction(Id)
local Factory = TestParent();
local ChildDirect = TestChild("DirectCall");
local ChildFactory1 = Factory:CreateChild("Factory1");
local ChildFactory2 = Factory:CreateChild("Factory2");
result = Ret()
return result
end
使用兩個C++暴露的物體(槽luabind)
void TestParent::RegisterToLua(lua_State* lua)
{
// Export our class with LuaBind
luabind::module(lua)
[
luabind::class_<TestParent>("TestParent")
.def(luabind::constructor<>())
.def("CreateChild", &TestParent::CreateChild)
];
}
void TestChild::RegisterToLua(lua_State* lua)
{
// Export our class with LuaBind
luabind::module(lua)
[
luabind::class_<TestChild>("TestChild")
.def(luabind::constructor<std::string>())
.def("GetValue", &TestChild::GetValue)
];
}
我調用函數
luabind::object obj = luabind::call_function<luabind::object>(LuaState, "TestFunction", IdParam);
if (obj.is_valid())
{
....
}
lua_gc(LuaState, LUA_GCCOLLECT, 0);
在lua_gc
通話只Factory
和ChildDirect
對象被破壞。 ChildFactory1
和ChildFactory2
仍然分配。 lua堆棧在luabind::call_function
後保持平衡(具有相同的值 - 5 - 一些表格)。
什麼問題? Factory創建的對象仍然以某種方式被引用?由誰 ?
擁有CreateChild體是
TestChild* TestParent::CreateChild(std::string strname)
{
return new TestChild(strname);
}
新構造的對象的所有權應由lua
對象採取和銷燬如果ChildFactory1或ChildFactory2是nil
-ed或超出範圍。
只是預感,但嘗試連續兩次調用lua_gc。 Lua使用增量垃圾收集器,它可以運行一段時間,所以可能只有一個調用不會執行完整的收集+定稿。 –