Sign in to follow this  

Luabind: call_function, shared_ptr and garbage collector

This topic is 3195 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello folks, I'm trying to instantiate class (base = cpp, derived = lua) with shared_ptr as holder using luabind::call_function. Everything seems to be okay for the first sight, but when i call lua_gc to collect garbage - shared_ptr instance of base class created via luabind::call_function reduces its use_call value to become 1 (as expected), but instance of script-derived class does not and stays 2. Then, i call virtual function - and get a crash on base instance, but not derived. Here's the code: ////////////////////////////////////////////////////////////////////// struct Base { Base() {} virtual ~Base() {} virtual void fn() { std::cout << "Base::fn()" << std::endl; } }; struct BaseWrap: Base, luabind::wrap_base { BaseWrap() : Base() {} virtual void fn() { luabind::call_member<void>(this, "fn"); } static void default_fn(Base* base) { base->Base::fn(); } }; void test_main(lua_State* L) { using namespace luabind; using boost::shared_ptr; module(L) [ class_<Base, shared_ptr<Base>, BaseWrap>("Base") .def(constructor<>()) .def("fn", &Base::fn, &BaseWrap::default_fn) ]; DOSTRING(L, "class 'Derived' (Base)\n" "function Derived:__init()\n" " Base.__init(self)\n" "end\n" "function Derived:fn()\n" " print('Derived:fn()')\n" "end\n" ); shared_ptr<Base> my_base_object = call_function<shared_ptr<Base>>(globals(L)["Base"]); shared_ptr<Base> my_derived_object = call_function<shared_ptr<Base>>(globals(L)["Derived"]); //my_base_object use_count = 2 //my_derived_object use_count = 2 lua_gc(L, LUA_GCCOLLECT, 0); //my_base_object use_count = 1 //my_derived_object use_count = 2 (why???) my_base_object->fn(); // crash here. if skip this line... my_derived_object->fn(); //...this one runs fine } ////////////////////////////////////////////////////////////////////// Is there something wrong? note: i was trying vs2005/gcc; both luabind and lua are built as static libs. [Edited by - tjr303 on March 15, 2009 5:22:51 PM]

Share this post


Link to post
Share on other sites

This topic is 3195 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this