ThePhD / sol2

Sol3 (sol2 v3.0) - a C++ <-> Lua API wrapper with advanced features and top notch performance - is here, and it's great! Documentation:
http://sol2.rtfd.io/
MIT License
4.12k stars 500 forks source link

Crash on metatable method error #1417

Open tommaisey opened 1 year ago

tommaisey commented 1 year ago

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.

ThePhD commented 1 year ago

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?

roman-orekhov commented 1 year ago

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