0

I'm having problems with luabind. I define a std::map to allocate objects created in lua. I publish this map as a global object in lua in this way:

luabind::globals(L)["g_SceneManager2D"] = this;

After that, this object is used into a function into lua, where many objects are created and inserted into this map. The problem comes when lua function ends and luabind returns the control to C++ side program, because automatically all contents of the map are lost.

I was looking for the error. I keep the lua context alive, so this object must exists.

Could you helpme??

Many thanks :)

Killrazor
  • 6,856
  • 15
  • 53
  • 69
  • If you post more code you'll probably get better answers: what type is `this` and the function declarations of the exported methods, also the Lua code that invokes those functions. – sbk Jan 17 '11 at 13:07

2 Answers2

1

I suggest use a shared_ptr<>(this) rather than raw this. boost::shared_from_this might help. Make sure your class is registered using Luabind too, and that the class_ is specified as held by a shared_ptr.

Another fun idea might be to make your Lua function just generate the "map" as a Lua table, return it, and you can iterate over it in C++ to build your std::map.

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
1

If I understand your problem correctly, it seems you are creating objects in Lua, which you then insert into the map (either through Lua or C++) and subsequently lose. Without some more code, it's hard to tell exactly what the problem is. However, I would first look to make sure that those objects are indeed being created (double check it) and then I would check to see that Lua isn't garbage collecting them. If Lua is indeed garbage collecting those objects, then you won't see them on the C++ side because they're, well, gone.

If it helps, I'm finishing up a project which does something similar. I had to create and retrieve C++ objects from Lua, but instead of creating the objects in Lua, I just called C++ functions to do it for me, sending any necessary data in the Lua call (bound by Luabind). Those (C++) functions indexed the objects by IDs into hash tables and the IDs were returned to Lua in case it needed to retrieve the object script-side for operations. This setup makes it easier (and safer) to handle memory stuff correctly and prevents Lua from garbage collecting your objects.

Gemini14
  • 6,246
  • 4
  • 28
  • 32