[英]How do I clean everything in lua stack from C++?
Although I fixed the bug by call as less time lua_getglobal
as I can, it's not good enough for long term. 尽管我通过调用
lua_getglobal
来尽可能地减少了该错误,但长期来看还不够好。 So does anyone know how do I clean everything in lua's stack to prevent memory management problems? 那么,有谁知道我该如何清理lua堆栈中的所有内容以防止内存管理问题?
---EDIT--- - -编辑 - -
From the experiments i just did, lua_settop
will clean up the table to the given value. 从我刚刚做的实验中,
lua_settop
会将表清理为给定值。 However, in the time i know how many items are there i want to remove, is lua_pop
more efficient? 但是,当我知道我要删除多少个项目时,
lua_pop
更有效吗?
A simple lua_settop(L, 0);
一个简单的
lua_settop(L, 0);
should do the trick. 应该可以。
A simple, albeit contrive, example say you have a lua_CFunction
: 一个简单的尽管有个例子,但示例说您有一个
lua_CFunction
:
int foo(lua_State *L)
{
// marshal some random data
int bar = luaL_checknumber(L, 1);
const char *baz = luaL_checkstring(L, 2);
// do foo's task
// completely clear the stack before return
lua_settop(L, 0);
return 0;
}
This is contrived because if foo
is called by the VM then cleanup is not necessary. 这是人为设计的,因为如果VM调用了
foo
则不需要清理。 But if you have C++ code calling foo
directly this may be necessary. 但是,如果您有直接调用
foo
C ++代码,则可能有必要。 At any rate hopefully this illustrates its calling context. 无论如何希望这可以说明其调用上下文。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.