failed when calling luaT_pushudata for the second time in a loop - c++

I am using lua's c api and had some trouble.
When I am doing some loops, luaT_pushudata always fails when I called it for the second time and said that
'no source available for lua_newuserdata() at 0x7ffff7ba85b3'
(when debugging in eclipse)
And when directly running the excusable, it just said '
segmentation fault (core dumped)
here is part of my codes (they are within a loop):
// establish the new tuple
tupleA temptupleA;
// cannot delete the buffer_char, the buffer char must be deallocate manually when erase the vector
unsigned char * buffer_char = new unsigned char[rect.width*rect.height*3];
glReadPixels(0, 0, rect.width, rect.height, GL_RGB, GL_UNSIGNED_BYTE, buffer_char);
temptupleA.pic = buffer_char;
temptupleA.action = new double[3];
temptupleA.reward = new double[1];
totalBuffer.push_back(temptupleA);
// set action
// get the Action
double * buffer = charA2fA(totalBuffer[totalStep].pic, raw_width*raw_height*3);
//std::cout<<buffer[raw_width*raw_height*3-1]<<std::endl;// check
THDoubleStorage *mystorage0 = THDoubleStorage_newWithData(&buffer[0], raw_width*raw_height*3);
THDoubleTensor* mytensor0 = THDoubleTensor_newWithStorage4d(
mystorage0, 0,
1, raw_width*raw_height*3,
3, raw_width*raw_height,
raw_width, raw_height,
raw_height, raw_width);
lua_getglobal(L, "returnAction");
std::cout<<totalStep; //check
luaT_pushudata(L, (void *)mytensor0, "torch.DoubleTensor"); // there is something wrong
std::cout<<" pass the luaT_pushudata"<<std::endl;
if(lua_pcall(L,1,3,0)) lua_error(L);
d->ctrl[0] = lua_tonumber(L,-3);
d->ctrl[1] = lua_tonumber(L,-2);
d->ctrl[2] = lua_tonumber(L,-1);
totalBuffer[totalStep].action[0]=d->ctrl[0];
totalBuffer[totalStep].action[1]=d->ctrl[1];
totalBuffer[totalStep].action[2]=d->ctrl[2];
lua_remove(L, -1);
lua_remove(L, -2);
lua_remove(L, -3);
THDoubleTensor_free(mytensor0);
delete [] buffer;
So what may be the cause of the problem??

I have found the problem: instead of writing lua_remove(L, -1), lua_remove(L, -2), lua_remove(L, -3) I should simply write lua_pop(L, 3) or write lua_remove(L, -1) for three times

luaT_pushudata steals a references, so you might access freed memory. Check with valgrind.

Related

Lua 5.2 Sandboxing in different objects with C API

Consider the following C++ code using the Lua C API:
#include <string>
#include <cassert>
#include <lua/lua.hpp>
class AwesomeThing
{
lua_State* _lua;
std::string _name;
public:
AwesomeThing(lua_State* L, const std::string& name, const std::string& luafile)
: _lua{ L },
_name{ name }
{
assert(luaL_loadfile(_lua, luafile.c_str()) == 0); // 1:chunk
lua_newtable(_lua); // 1:chunk, 2:tbl
lua_newtable(_lua); // 1:chunk, 2:tbl, 3:tbl(mt)
lua_getglobal(_lua, "_G"); // 1:chunk, 2: tbl, 3:tbl(mt), 4:_G
lua_setfield(_lua, 3, "__index"); // 1:chunk, 2: tbl, 3:tbl(mt)
lua_setmetatable(_lua, 2); // 1:chunk, 2: tbl
lua_setupvalue(_lua, -2, 1); // 1:chunk
if (lua_pcall(_lua, 0, 0, 0) != 0) // compiled chunk
{
auto error = lua_tostring(_lua, -1);
throw std::runtime_error(error);
}
lua_setglobal(_lua, _name.c_str()); // empty stack
}
void init()
{
lua_getglobal(_lua, _name.c_str()); // 1:env
assert(lua_isnil(_lua, 1) == 0);
lua_getfield(_lua, 1, "onInit"); // 1:env, 2:func
assert(lua_isnil(_lua, 2) == 0);
assert(lua_isfunction(_lua, 2) == 1);
assert(lua_pcall(_lua, 0, LUA_MULTRET, 0) == 0); // 1:env, 2:retval
lua_pop(_lua, 1); // -1:env
lua_pop(_lua, 1); // empty stack
assert(lua_gettop(_lua) == 0);
}
};
int main()
{
lua_State* L = luaL_newstate();
luaL_openlibs(L);
AwesomeThing at1(L, "thing1", "file1.lua");
AwesomeThing at2(L, "thing2", "file2.lua");
at1.init();
at2.init();
return 0;
}
With two very basic Lua files:
file1.lua
function onInit()
print("init file1")
end
file2.lua
function onInit()
print("init file2")
end
As is, I get an error in at2's constructor call at lua_pcall: attempt to call table value
When I comment out all references/calls to at2, I instead get an error in at1's init() at lua_getfield(_lua, 1, "onInit"): PANIC: unprotected error in call to Lua API (attempt to index a nil value)
I feel like there's something fundamental I'm missing in the way I'm handling the sandboxing. I've tried my best to follow a few other Lua 5.2 sandboxing examples I've found online, but so far nothing has helped.
After messing around with the code myself, I was able to fix it and the errors seem to come from just a few errors.
lua_pcall pops the called function from the stack, but in both cases in your code you assume the function is still on the stack after lua_pcall. This results in bad stack manipulation.
In the constructor, you apparently try to store a reference to the chunk (function) instead of the environment table. This doesn't even work though, because the function was already popped. If it did work, the lua_getfield call in init() wouldn't work as intended since the chunk doesn't have a field named onInit -- the environment table does.
Fixing the constructor involves creating the environment table and loading the chunk in the opposite order, so that the environment table is left on the stack after the function call:
lua_newtable(_lua); // 1:tbl
assert(luaL_loadfile(_lua, luafile.c_str()) == 0); // 1:tbl, 2:chunk
lua_newtable(_lua); // 1:tbl, 2:chunk, 3:tbl(mt)
lua_getglobal(_lua, "_G"); // 1:tbl, 2:chunk, 3:tbl(mt), 4:_G
lua_setfield(_lua, 3, "__index"); // 1:tbl, 2:chunk, 3:tbl(mt)
lua_setmetatable(_lua, 1); // 1:tbl, 2:chunk
lua_pushvalue(_lua, 1); // 1:tbl, 2:chunk, 3:tbl
lua_setupvalue(_lua, -2, 1); // 1:tbl, 2:chunk
if (lua_pcall(_lua, 0, 0, 0) != 0) // compiled chunk
{
auto error = lua_tostring(_lua, -1);
throw std::runtime_error(error);
}
// 1:tbl
lua_setglobal(_lua, _name.c_str()); // empty stack
Then in init(), since you use LUA_MULTRET, just clear the stack by replacing both pop calls with lua_settop(_lua, 0).

lua - store closure in C, invoke async in C

I need an idea, how I can store lua closures to invoke them asynchronously later.
my first idea was lua_tocfunction but a closure is not a cfunction and cannot be invoked from C directly
second idea was to save the closure in the metatable, that I can push it and call it later, but it seems, that I cannot copy a closure. (Error: attempt to index a function value).
So I need your help please. How can I store a closure?
I admit, that I did not completely understand why there is an __index field in my lua ctor as I've copied that part from somewhere.
By the way: the program without onrender worked as expected. I'm using qt gui and the lua-states are closed, after qt's main loop, thus the created window is not going to be delete by __gc after the script.
bootstrap.lua
local w = w_render() -- create window object
w:show()
w:onrender(function()
print('render')
end)
w_lua.cpp
// chlua_* are helper macros/templates/methods
// 1: self
// 2: render closure
int w_render_onrender(lua_State *L) {
auto *self = chlua_this<GLWindow *>(L, 1, w_render_table);
lua_pushvalue(L, 2); // copy closure to top
lua_setfield(L, 2, "onrender_cb"); // save closure in metatable
// !!! ERROR: attempt to index a function value
self->onrender([L](){
lua_getfield(L, 2, "onrender_cb");
qDebug() << "onrender";
lua_call(L, 0, 0);
});
return 0;
}
// Creates the object
int w_render(lua_State *L) {
auto *&self = chlua_newuserdata<GLWindow *>(L);
self = new GLWindow;
if (luaL_newmetatable(L, w_render_table)) {
luaL_setfuncs(L, w_render_methods, 0);
lua_pushvalue(L, -1);
lua_setfield(L, -2, "__index");
}
lua_setmetatable(L, -2);
return 1;
}
It looks like your problem is stemming from using the wrong indices and attempting to set/get fields on the wrong lua object on the stack. Assuming the udata representing your GLWindow * is first followed by the lua closure second, try changing the code like this:
int w_render_onrender(lua_State *L)
{
luaL_checkudata(L, 1, w_render_table);
luaL_checktype(L, 2, LUA_TFUNCTION);
auto *self = chlua_this<GLWindow *>(L, 1, w_render_table);
lua_getmetatable(L, 1);
lua_insert(L, -2); // GLWindow GLWindow_mt lua_closure
lua_setfield(L, -2, "onrender_cb"); // save closure in metatable
self->onrender([L]()
{
luaL_checkudata(L, 1, w_render_table);
// assuming GLWindow udata is self and onrender_cb is your lua closure above
// access GLWindow.onrender_cb through GLWindows's metatable
lua_getfield(L, 1, "onrender_cb");
qDebug() << "onrender";
luaL_checktype(L, -1, LUA_TFUNCTION); // Just to be sure
lua_call(L, 0, 0);
});
return 0;
}
Edit: After thinking about this some more, it probably makes more sense to create a lua reference using luaL_ref. This way you don't have to care what happens to be on the stack when self->onrender actually runs, which I'm assuming is async:
int w_render_onrender(lua_State *L)
{
luaL_checkudata(L, 1, w_render_table);
luaL_checktype(L, 2, LUA_TFUNCTION);
auto *self = chlua_this<GLWindow *>(L, 1, w_render_table);
auto lua_cb = luaL_ref(L, LUA_REGISTRYINDEX);
// just to check that what's on the stack shouldn't matter
lua_settop(L, 0);
self->onrender([L, lua_cb]()
{
lua_rawgeti(L, LUA_REGISTRYINDEX, lua_cb);
luaL_checktype(L, -1, LUA_TFUNCTION); // Just to be sure
qDebug() << "onrender";
lua_call(L, 0, 0);
luaL_unref(L, LUA_REGISTRYINDEX, lua_cb); // assuming you're done with it
});
return 0;
}

cocos2dx 3.x schedule error

When I try implement add one node per n seconds, the procedure crashes.
if I just run runBlock(0.0f), the procedure runs normally.
This is my code
MainLayer.cpp
bool MainLayer::init(){
if (!Layer::init()){
return false;
}
block_array = new std::vector<block*>();
Size visibleSize = Director::getInstance()->getVisibleSize();
auto body = PhysicsBody::createEdgeBox(visibleSize,PHYSICSBODY_MATERIAL_DEFAULT, 3.0);
body->setCategoryBitmask(0x0001);
body->setCollisionBitmask(0x0001);
body->setContactTestBitmask(0x0000);
body->getShape(0)->setRestitution(0.0);
auto edgeNode = Node::create();
edgeNode->setPosition(Vec2(visibleSize.width / 2, visibleSize.height / 2));
edgeNode->setPhysicsBody(body);
this->addChild(edgeNode);
auto sp = Sprite::create("CloseNormal.png");
sp->setTag(PLAYER);
auto sp_body = PhysicsBody::createCircle(sp->getContentSize().width / 2);
sp_body->setCategoryBitmask(0x003);
sp_body->setCollisionBitmask(0x003);
sp_body->setContactTestBitmask(0x001);
sp->setPhysicsBody(sp_body);
sp->setPosition(visibleSize / 2);
this->addChild(sp);
initBlock();
/**
When run function "runBlock", process crash!!
*/
schedule(schedule_selector(MainLayer::runBlock), 5.0f, CC_REPEAT_FOREVER, 0.0f);
//runBlock(0.0f); if just run it, everything is right
return true;
}
void MainLayer::initBlock(){
block_array->push_back(new block1()); //block is other class, it has no problem
block_array->push_back(new block2());
block_array->push_back(new block3());
}
void MainLayer::runBlock(float dt){
Size size = Director::getInstance()->getVisibleSize();
int len = block_array->size();
block* bl;
do
{
int rand = floor(CCRANDOM_0_1()*len);
if (rand == len){
rand -= 1;
}
bl = (*block_array)[rand];
} while (bl->node->getParent()); //Crash in here!!!!!
bl->come(Vec2(size.width*1.5, 0));
this->addChild(bl->node);
}
I use VS2013. The error info is
Without exception handling in 0x009D5402(in test.exe) 0xC0000005: Access violation when reading location 0xFEEF0012
I have debug. "bl->node" is allocated memory. So I don't think it is NULL pointer error. However, I don't know what reason cause the crash.
Please help me, Thanks!
After a few frames all your blocks will have parents and the while loop will never exit.
I don't really understand what it is you're trying to accomplish here, but might I suggest keeping two arrays, filling the first in initBlocks(), randomly picking one in runBlock(), processing it, and moving it to the second array.
This way you don't need the while loop, which wastes a lot of time if you keep picking a block that you've already processed.
Do it this way:
void MainLayer::runBlock(float dt)
{
Size size = Director::getInstance()->getVisibleSize();
//waiting_block_array = block_array when you initialize
if (waiting_block_array)
{
int rand = random (0, waiting_block_array.size () - 1);
addChild (waiting_block_array[rand]);
waiting_block_array[rand]->come(Vec2(size.width*1.5, 0));
this->addChild(waiting_block_array[rand]->node);
waiting_block_array.erase (waiting_block_array.begin () + rand);
}

Migration of JCoFunction in sapjco 3.x

I'm migrating from JCO2.x to 3.x. I have rewritten almost the whole code, but with this I cannot move.
Original 2.x code:
JCO.ParameterList input = new JCO.ParameterList();
input.addInfo("APP_AREA", JCO.TYPE_STRING, 0, 0, 0, JCO.IMPORT_PARAMETER, null);
input.addInfo("XML", JCO.TYPE_STRING, 0, 0, 0, JCO.IMPORT_PARAMETER, null);
JCO.Function function = new JCO.Function(
BAPI_NAMESPACE + "ZZZ",
input, // input
new JCO.ParameterList(), // output
new JCO.ParameterList() // tables
);
My proposed code:
JCoParameterList input = new JCoParameterList();
input.addInfo("APP_AREA", JCO.TYPE_STRING, 0, 0, 0, JCO.IMPORT_PARAMETER, null);
input.addInfo("XML", JCO.TYPE_STRING, 0, 0, 0, JCO.IMPORT_PARAMETER, null);
JCoFunction function = new JCoFunction(
BAPI_NAMESPACE + "ZZZ",
input, // input
new JCoParameterList(), // output
new JCoParameterList() // tables
);
Thing is that JCoFunction cannot be instantiated in this form in 3.x. Should I create function template? Thank you for any hints.
.... a little late response...
You have to get the JcoFunction from your JcoDestination.
e.g.
JCoDestination destination = JCoDestinationManager
.getDestination(destinationName);
JCoRepository repository = destination.getRepository();
JCoFunctionTemplate template = repository.getFunctionTemplate(functionName);
JCoFunction function = template.getFunction();
JCoParameterList input = function_.getImportParameterList();
//set data on input
//execute the function
function.execute(destination);
//access the output
JCoParameterList output = function_.getExportParameterList();
JCoParameterList table = function_.getTableParameterList();

Using lua_call a lot of times in c++ function

first of all I'm sorry for my english.
My question is about how to use lua_call more than one time in C++ function. I have a program that uses lua as primary language, but it accept c++ plugins to add functionalities. I want to call a LUA function from c++, and call that c++ function in LUA Runtime.
I want to write a c++ function with a progress while is working, then pass this progress to a LUA function which is responsible to show that progress to user.
For now I've a test function in LUA:
function ShowText(text)
Dialog.Message("Hi", text);
return true;
end
and a c++ function:
static int Test(lua_State *L){
lua_pushstring(L, "Hi There");
lua_call(L, 1, 1);
lua_pushstring(L, "Again");
lua_call(L, 1, 1);
return 0;
}
Then i call this function from LUA using:
Test.Test(ShowText);
All works fine with the first lua_call but then the LUA pile is cleared, function dissapear and the second lua_call try to use the return boolean of first call instead function.
i want something like this:
static int Test(lua_State *L){
int total = 10;
for (int j; j<total; j++){
lua_pushnumber(L, j);
lua_pushnumber(L, j);
lua_call(L, 2, 1);
bool continue = IRLUA_PLUGIN_CheckBoolean(L, -1);
lua_pop(L, 1); //Delete the last boolean
if (continue == false){
break;
}
}
return 0;
}
and in LUA:
function ShowProgress(actual, final)
local percent = (actual/final)*100;
Dialog.Message("Working", "I'm in "..actual.." from "..final.." ("..percent.."%)");
return true;
end
NOTES:
Dialog.Message is a function of the program tha i'm using to show a message. Is like MessageBox(NULL, Text, Title, MB_OK); in c++.
IRLUA_PLUGIN_CheckBoolean is a function of plugin SDK that check if argument is booleand and return its value, or return an error if not.
I can do it with lua_getfield(L, LUA_GLOBALSINDEX , "FunctionName");, but is not what i want.
Someone knows how to do it?
You have understood the problem well. Here is how you fix it.
In your first example, lua_call pops the function from the stack so you need to duplicate it first. Also, the boolean returned by the function is useless, so you need to pop it or just not ask it to lua_call by setting the last argument to 0:
static int Test(lua_State *L) {
lua_pushvalue(L, 1); /* duplicate function */
lua_pushstring(L, "Hi There");
lua_call(L, 1, 0);
lua_pushstring(L, "Again");
lua_call(L, 1, 0);
return 0;
}
Now applying that to your second example:
static int Test(lua_State *L) {
int total = 10;
for (int j = 0; j<total; j++) {
lua_pushvalue(L, 1); /* duplicate function */
lua_pushnumber(L, j);
lua_pushnumber(L, total);
lua_call(L, 2, 1);
bool keep_going = IRLUA_PLUGIN_CheckBoolean(L, -1);
lua_pop(L, 1); /* pop the boolean */
if (keep_going == false) {
break;
}
}
return 0;
}
(I have fixed a few other issues with your code: the second number passed should probably be total, not j, you don't want to use continue as a variable name...)