Re: Re move an object from the lua state!
Teto <[email protected]> Tue, 2 Aug 2011 11:08:57 +0200
| Newsgroups | gmane.comp.lang.lua.bind.user |
|---|---|
| Message-ID | <CADHp1NyhqZawvnzrVUUkBY916FFED2GdT__2RNTaUjo1fWa5PQ@mail.gmail.com> |
I am not sure but I would say allocate your enemy from C++ side with a factory for example instead of using luabind bound constructor. The moment you call Enemy.__init(self, name); you create something lua will collect on lua_close. Plus you override your C++ function "Enemy * CreateEnemy(std::string const & name)" by creating a lua one "function CreateEnemy(name)". when you do "delete e;" I would say you delete from C++ memory allocated by lua via "return(Zombie(name));". When you call lua_close, lua still has references towards that zombie and tries to delete it despite you've already deleted it ? I may be wrong but that's how I see it. Some posts that might interest you: http://lua-list.2524044.n2.nabble.com/Lunar-C-and-premature-object-destruction-td3193945.html http://stackoverflow.com/questions/1047212/detecting-stale-c-references-in-lua On Tue, Aug 2, 2011 at 6:37 AM, Sympaval <[email protected]> wrote: > > Hi all! > > I a problem since two days now, while integreting lua to C++. I look at all > message corresponding to that issue here, but the answers are not clearly > explaned to help me, and here I would like to get the ritght way to achieve > what I would like to do. > > In fact I have a c++ class wraped with luabind and exported to lua. I would > like to be able to create a new instance of my class in lua, get it and > delete it from c++. > Actually i can create new instances of my object, and delete them without > problem, but when I close the lua state with lua_close(myLuaState), the > programme segfault. When i comment thje lua_close function all thinks are > ok. I realise that it's beacause when the lua_close is called, it tries to > call the destructor of all objects in the lua state. > Then I would like to delete the object from the c++ side when i want, and > remove it from the lua state. How can i achieve that? > This question has been already posted here in another way, but i didn't find > a answer which solve my problem. > I read the programming in lua documentation but I do not see how to proceed. > This is my code. > > extern "C" { > #include <lua.h> > } > > #include <iostream> > #include <lua.hpp> > #include <luabind.hpp> > #include <stdio.h> > > class Enemy { > private: > std::string name; > > public: > Enemy(const std::string& n) > : name(n) > { > std::cout << "New enemy born with name : " << name << ".\n"; > } > > Enemy(Enemy const & other) > : name(other.name) > { > std::cout << "Copy of enemy made : " << other.name << ".\n"; > } > > const std::string& getName() const > { > std::cout << "Enamy name has been asked and given.\n"; > return name; > } > > void setName(const std::string& n) { > name = n; > } > > virtual int update() > { > std::cout << "Enemy::update() called.\n"; > return (43); > } > }; > > class EnemyWrapper : public Enemy, public luabind::wrap_base { > public: > EnemyWrapper(const std::string& n) > : Enemy(n) { > } > > virtual int update() { > return (call<int>("update")); > } > } > > Enemy * CreateEnemy(std::string const & name) > { > return new Enemy(name); > } > > > int main() { > lua_State * L = lua_open() > luabind::open(L); > luaL_openlibs(L); > > luabind::module(L) [ > luabind::class_<Enemy, EnemyWrapper>("Enemy") > .def(luabind::constructor<const std::string&>()) > .property("name", &Enemy::getName, &Enemy::setName) > .def("update", &Enemy::update, > &EnemyWrapper::default_update) > .def("CreateEnemy", &CreateEnemy) > ]; > > if (luaL_dofile(L, "wrapping.lua") != 0) > std::cout << lua_tostring(L, -1) << std::endl; > int i = 1; > for (; i <= 10; ++i) > { > std::cout << i << " " ; > Enemy * e = luabind::call_function<Enemy *>(L, "CreateEnemy", "toto"); > printf("%p :", e); > std::cout << e->update() << std::endl; > sleep(1); > delete e; > } > lua_close(L); > } > > Lua code: > print("Start the script"); > > class 'Zombie' (Enemy) > > function Zombie:__init(name) > Enemy.__init(self, name); > end > > function Zombie:update() > print('Zombie:update() called.'); > self.talk(); > return (72); > end > > function Zombie:talk() > print("Yeah I'm a monster"); > end > > function CreateEnemy(name) > return(Zombie(name)); > end > > > > > > > > > > -- > View this message in context: http://old.nabble.com/Remove-an-object-from-the-lua-state%21-tp32174811p32174811.html > Sent from the Lua C++ Bind mailing list archive at Nabble.com. > > > ------------------------------------------------------------------------------ > BlackBerry® DevCon Americas, Oct. 18-20, San Francisco, CA > The must-attend event for mobile developers. Connect with experts. > Get tools for creating Super Apps. See the latest technologies. > Sessions, hands-on labs, demos & much more. Register early & save! > http://p.sf.net/sfu/rim-blackberry-1 > _______________________________________________ > luabind-user mailing list > [email protected] > https://lists.sourceforge.net/lists/listinfo/luabind-user > ------------------------------------------------------------------------------ BlackBerry® DevCon Americas, Oct. 18-20, San Francisco, CA The must-attend event for mobile developers. Connect with experts. Get tools for creating Super Apps. See the latest technologies. Sessions, hands-on labs, demos & much more. Register early & save! http://p.sf.net/sfu/rim-blackberry-1 _______________________________________________ luabind-user mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/luabind-user