sol2 icon indicating copy to clipboard operation
sol2 copied to clipboard

Crash on metatable method error

Open tommaisey opened this issue 3 years ago • 2 comments

I'm using Lua 5.4.4 and Sol 3.3.0. The following program crashes with an uncaught exception, despite my handler.

sol::state lua;
lua.open_libraries (sol::lib::base);

lua.script (R"(
    t = setmetatable({}, {
        __newindex = function() error('ouch!') end
    })
)");

try {
    lua["t"]["x"] = 10;
} catch (const std::exception& e) {
    std::cout << "The error is: " << e.what() << "\n";
}

The exception is attempt to call a nil value, and it seems to originate in basic_table_core::traverse_set when the push_popper_n goes out of scope, i.e. while the stack is unwinding.

tommaisey avatar Oct 27 '22 10:10 tommaisey

Catching a Lua error as an exception is always tricky, and exception-related mechanisms have been broken for a long time now in sol2 due to the many different issues with triggering unwinding through a C-style library.

As a first question, did you compile Lua-as-C++, and did you enable SOL_USING_CXX_LUA as a preprocessor define for the whole build?

ThePhD avatar Nov 09 '22 23:11 ThePhD

Version in branch develop seems to be ok with this code.

roman-orekhov avatar Jul 07 '23 00:07 roman-orekhov