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

Loss of idempotence of user type declarationd post v3.2.3 #1523

Open
ormshaw opened this issue Sep 8, 2023 · 3 comments
Open

Loss of idempotence of user type declarationd post v3.2.3 #1523

ormshaw opened this issue Sep 8, 2023 · 3 comments

Comments

@ormshaw
Copy link

ormshaw commented Sep 8, 2023

I am working on Fedora 37 using gcc 12.3.1 and lua 5.4.6. The following code works as expected with sol v 3.2.3.

#include "TestClass.h"

#define SOL_ALL_SAFETIES_ON 1
#include "sol/sol.hpp"

#include
#include

int main()
{
std::string script
{
R"(
print(testClass)
testClass:greet()
)"
};

try
{
    sol::state lua;
    lua.open_libraries();

    for(int i{0}; i<16; ++i)
    {
        sol::usertype<TestClass> luaUserType{lua.new_usertype<TestClass>("TestClass")};
        luaUserType.set("TestClass", sol::constructors<TestClass()>());
        luaUserType.set("greet", &TestClass::greet);

        TestClass testClass{};
        lua["testClass"] = &testClass;

        auto result{lua.safe_script(script)};
    }
}
catch( const sol::error& e )
{
    std::cout << "An unexpected error has occurred: " << e.what() << std::endl;
}

}

The output from this code is as follows:
sol.TestClass*: 0x2167dc8
Hello World!!!
userdata: 0x2169f98
Hello World!!!
userdata: 0x216b188
Hello World!!!
userdata: 0x216c378
Hello World!!!
userdata: 0x216d568
Hello World!!!
userdata: 0x21678d8
Hello World!!!
userdata: 0x2169a18
Hello World!!!
userdata: 0x216adf8
Hello World!!!
userdata: 0x216df18
Hello World!!!
userdata: 0x216eff8
Hello World!!!
userdata: 0x2170188
Hello World!!!
userdata: 0x2171378
Hello World!!!
userdata: 0x2172568
Hello World!!!
userdata: 0x216d088
Hello World!!!
userdata: 0x216c378
Hello World!!!
userdata: 0x2161cd8
Hello World!!!

If I change to using sol 3.3.x or 4.x the out put from the program is:

/home/ian/projects/lua/sol/build/source/idempotence> /home/ian/projects/lua/sol/build/source/idempotence/indempotence
sol.TestClass*: 0x7e2cd8
Hello World!!!
sol.TestClass*: 0x7e5718
Hello World!!!
sol.TestClass*: 0x7e7178
Hello World!!!
sol.TestClass*: 0x7e2998
An unexpected error has occurred: sol: runtime error: [string "..."]:3: attempt to index a sol.TestClass* value (global 'testClass')
stack traceback:
[string "..."]:3: in main chunk
[sol2] An error occurred and has been passed to an error handler: sol: runtime error: [string "..."]:3: attempt to index a sol.TestClass* value (global 'testClass')
stack traceback:
[string "..."]:3: in main chunk

I have seen similar code failing the first time the user type was re-declared. An other instance it was after 10 re-declarations.

My expectations is that the example code should work regardless of the number of re-declarations. I.e. it should be idempotent.

@ormshaw
Copy link
Author

ormshaw commented Sep 8, 2023

The same results are seen in Fedora 38 with gcc 13.2.1

@congard
Copy link

congard commented Sep 25, 2023

Creating multiple non-shared environments (i.e., without a parent) and then creating new usertypes within them results in quite similar behavior

@devgs
Copy link

devgs commented Nov 8, 2023

Duplicate of #1492

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