Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
deadlocklogic opened this issue Sep 23, 2024 · 5 comments
Open

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

deadlocklogic opened this issue Sep 23, 2024 · 5 comments

Comments

@deadlocklogic
Copy link
Contributor

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
Copy link
Contributor Author

@Smertig Ever noticed this behavior change or I am missing something?

@Smertig
Copy link
Contributor

Smertig commented Sep 25, 2024

@deadlocklogic I worked with this library too long ago, don't remember anything. Sorry.

@Spacechild1
Copy link

Spacechild1 commented Feb 12, 2025

You can't pass nil to a function that expects a plain Test* either. std::shared_ptr<Test> and Text* are both nullable and sol2 seems to treat them consistently. After all, nil is not necessarily the same thing as nullptr.

Note that you can use sol::optional for optional function arguments:

lua["f"] = [](sol::optional<std::shared_ptr<Test>> arg) {
    auto ptr = arg.value_or(nullptr);
};

lua.script("f(nil)"); // ok!

See also #1592 (comment)

AFAICT the use of sol::optional as function arguments isn't really officially documented anywhere... Although this issue is technically a duplicate of #1592, we should keep it open until the documentation has been updated. Also, it would be good to know if that behavior has changed in the past. If yes, this should be documented as well!

@deadlocklogic
Copy link
Contributor Author

@Spacechild1 I had an old codebase using sol and which was accepting nil for shared_ptr fine.
As far as I know, pointers accept nil as nullptr, don't know if something changed.

@Spacechild1
Copy link

As far as I know, pointers accept nil as nullptr, don't know if something changed.

Ah, you're right! I just tested with 3.3.1. It works as long as you explicitly pass nil.

lua["f"] = [](Test* arg) {
    print(arg)
};

lua.script("f(nil)"); // ok!

lua.script("f()"); // error!

If sol2 used to also accept nil for shared_ptr, it would be interesting to know when the breaking change happened. In the meantime, just use the std::optional trick.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants