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

strange error in coroutine. #131

Open
yihuang opened this issue Aug 19, 2016 · 4 comments
Open

strange error in coroutine. #131

yihuang opened this issue Aug 19, 2016 · 4 comments

Comments

@yihuang
Copy link

yihuang commented Aug 19, 2016

#include "lua.h"
#include "LuaIntf/LuaIntf.h"

using namespace LuaIntf;

class Test
{
public:
    Test (lua_State* L) {
        value = LuaRef::createTable(L);
    }
    virtual ~Test () = default;

    LuaRef value;
};

int main() {
    lua_State* L = luaL_newstate();
    luaL_openlibs(L);
    LuaBinding(L)
        .beginClass<Test>("Test")
        .addConstructor(LUA_ARGS(lua_State*))
            .addVariable("value", &Test::value)
        .endClass();
    if (luaL_dofile(L, "main.lua")) {
        printf("%s\n", lua_tostring(L, -1));
    }
}

main.lua:

t = Test()
print(t.value)
local co = coroutine.create(function()
    print(t.value)
end)
coroutine.resume(co)

Output:

table: 0x7f8a33d12e30
userdata: 0x7f8a33d12ee8

In coroutine, the LuaRef is returned as userdata.

@yihuang
Copy link
Author

yihuang commented Aug 20, 2016

I get it, LuaRef store the lua_State it created in, but different lua coroutine has different lua_State instance. But LuaRef::pushToStack use the lua_State it stores, not the current coroutine state.
Add a lua_State argument to LuaRef::pushToStack can solve this specific problem, but I wonder is there other similar situations.

@yihuang
Copy link
Author

yihuang commented Aug 20, 2016

There is another problem with coroutine which maybe more annoying, a LuaRef may hold a coroutine's lua_State which could be freed when the coroutine is gc-ed.

yihuang added a commit to yihuang/lua-intf that referenced this issue Aug 20, 2016
@yihuang
Copy link
Author

yihuang commented Aug 20, 2016

I've changed LuaRef to only store main thread state, the problem should be fixed now.

For 5.2, it just works.
For 5.1, your first usage of LuaRef should be in main thread, since LuaBinding use LuaRef inside, call LuaBinding in main thread works too.

@i0gan
Copy link

i0gan commented Sep 28, 2023

I got the same problem! Thanks for commited a pull request to solve this problem.

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

2 participants