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

shared_ptr use_count corruption #18

Open
tripleslash opened this issue Aug 3, 2013 · 0 comments
Open

shared_ptr use_count corruption #18

tripleslash opened this issue Aug 3, 2013 · 0 comments

Comments

@tripleslash
Copy link

Luabind causes a shared_ptr corruption when you do this in lua:

local gSharedObject = nil;
function Testfunction()
    gSharedObject = SharedObject.Create();
    return gSharedObject;
end

where the C++ export looks like that:

class SharedObject {
public:
    SharedObject() {}

    static boost::shared_ptr<SharedObject> Create() {
        return boost::make_shared<SharedObject>();
    }
}

luabind::module(L) [
    luabind::class_<SharedObject, boost::shared_ptr<SharedObject>>("SharedObject")
        .scope [ luabind::def("Create", &SharedObject::Create) ]
];

Now when you do the following you will get a use_count == 1 where it should actually be 2 because lua should hold a shared_ptr and your code should:

boost::shared_ptr<SharedObject> obj = luabind::call_function<boost::shared_ptr<SharedObject>>(L, "Testfunction");
std::cout << obj.use_count() << std::endl;
// prints 1 but should actually be 2, one from the current scope and one in lua globals, because the lua state is still alive... ?!?

This is a huge problem when your GC runs because it always thinks the shared_ptr is the only one in use and it will always release objects created like that, not matter if a ptr is used by the c++ code atm.

Example:

boost::shared_ptr<SharedObject> obj = luabind::call_function<boost::shared_ptr<SharedObject>>(L, "Testfunction");
std::cout << obj.use_count() << std::endl;

lua_close(L); // invokes ~SharedObject -_-

std::cout << obj.use_count() << std::endl; // still 1

The problem exists in lua 5.1 and 5.2, everywhere I checked

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

1 participant