sol2 icon indicating copy to clipboard operation
sol2 copied to clipboard

Can't pass nil for smart pointers like std::shared_ptr

Open deadlocklogic opened this issue 1 year ago • 5 comments

Why we can't pass nil to std::shared_ptr without registering a new overload. This behavior used to work previously according to my old tests, not anymore. Consider this example:

#define SOL_ALL_SAFETIES_ON 1
#include <sol/sol.hpp>

struct Test {};

int main() {
    sol::state lua;
    lua.open_libraries(sol::lib::base);
    lua.new_usertype<::Test>("Test");

    lua["f"] = [](std::shared_ptr<Test> arg) {};
    lua.script("f(nil)");

    return 0;
}

Throws this error:

[sol2] An error occurred and has been passed to an error handler: sol: runtime error: stack index 1, expected userdata, received nil: value is not a userdata (bad argument into 'void(std::shared_ptr<Test>)')
stack traceback:
	[C]: in function 'base.f'
	[string "f(nil)"]:1: in main chunk
terminate called after throwing an instance of 'sol::error'
  what():  sol: runtime error: stack index 1, expected userdata, received nil: value is not a userdata (bad argument into 'void(std::shared_ptr<Test>)')
stack traceback:
	[C]: in function 'base.f'
	[string "f(nil)"]:1: in main chunk
Program terminated with signal: SIGSEGV

@ThePhD Any thoughts?

deadlocklogic avatar Sep 23 '24 07:09 deadlocklogic