简体   繁体   中英

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. So does anyone know how do I clean everything in lua's stack to prevent memory management problems?

---EDIT---
From the experiments i just did, lua_settop will clean up the table to the given value. However, in the time i know how many items are there i want to remove, is lua_pop more efficient?

A simple lua_settop(L, 0); should do the trick.

A simple, albeit contrive, example say you have a 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. But if you have C++ code calling foo directly this may be necessary. At any rate hopefully this illustrates its calling context.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM