有没有一种方法可以在不“缓存”它们的情况下将在堆上分配的对象返回给Lua?
考虑以下代码:
如果我返回一个分配的内存字符串,我必须删除它。有没有一种方法可以将所有权转移给lua?
或者,是否有可能使用lua_pushstring(...)自己实现字符串返回并获取lua_state*?
考虑以下代码:
class foo
{
char const* bar() const
{
char* s = malloc(...);
...
return s; // << Leak. How to transfer the ownership of 's' to lua?
}
};
如果我返回一个分配的内存字符串,我必须删除它。有没有一种方法可以将所有权转移给lua?
或者,是否有可能使用lua_pushstring(...)自己实现字符串返回并获取lua_state*?
push_string
的问题在于我在 C++ 函数中无法访问lua_state
。 - user3647854