Jump to content
  • Advertisement
Sign in to follow this  

Luabind: call_function, shared_ptr and garbage collector

This topic is 3477 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
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!