我使用lua_pcall
來調用某個函數,我想要捕獲錯誤。 在某些情況下,錯誤似乎會丟失。這怎麼可能發生? 這適用於我使用錯誤處理程序和不使用時的情況。在這兩種情況下,堆棧的頂部都不是字符串。lua_pcall錯誤消息丟失
C代碼:
lua_getglobal(L, "debug");
lua_getfield(L, -1, "traceback");
lua_replace(L, -2);
lua_rawgeti(L, LUA_REGISTRYINDEX, my_func_index);
// now push n_in number of values on the stack
luaT_stackdump(L);
int pcall_ret = lua_pcall(L, n_in, n_out, -n_in - 2);
// lua_pcall will consume n_in+1 values from the stack.
if(pcall_ret != 0) {
const char* errmsg = lua_tostring(L, -1);
if(!errmsg) {
errmsg = "(No Lua error message.)";
printf("Unexpected Lua stack:\n");
luaT_stackdump(L);
}
printf("Lua error code %i: %s\n", pcall_ret, errmsg);
lua_pop(L, 2); // remove error and debug.traceback from the stack
return ...;
}
// now we got n_out values on the stack
這就是所謂的Lua的功能看起來像這樣(用於測試):
function (x, W, b, index)
print "hi from Lua func"
A = torch.rand(15, 12)
B = torch.rand(12, 23)
C = torch.dot(A, B)
end
當它調用torch.dot
這多少會得到一個錯誤。 但我不完全知道爲什麼。我沒有得到任何有意義的錯誤。 這就是我的問題所在。
輸出:
1. Lua object type: function
2. Lua object type: function
3. userdata 4165a368 [torch.FloatTensor]
4. userdata 4165a390 [torch.FloatTensor]
5. userdata 4165a230 [torch.FloatTensor]
6. userdata 4165a258 [torch.CharTensor]
---------------------------------------------
hi from Lua func
Unexpected Lua stack:
1. Lua object type: function
2. userdata 40ea1230 [torch.DoubleTensor]
---------------------------------------------
Lua error code 2: (No Lua error message.)
或者,也許我的代碼是正確的,它真的應該在這裏返回錯誤字符串?所以在撥打torch.dot
時可能會出現一些內存損壞,即某些事情會被搞砸了?