Does CEGUI Destroy the lua_state automatically?

For help with general CEGUI usage:
- Questions about the usage of CEGUI and its features, if not explained in the documentation.
- Problems with the CMAKE configuration or problems occuring during the build process/compilation.
- Errors or unexpected behaviour.

Moderators: CEGUI MVP, CEGUI Team

pizzazhang
Not too shy to talk
Not too shy to talk
Posts: 23
Joined: Wed Apr 13, 2011 14:54
Location: Shanghai/China

Does CEGUI Destroy the lua_state automatically?

Postby pizzazhang » Sun Jun 05, 2011 03:37

Hi,
I use the LuaScriptModule of CEGUI in my code:

Code: Select all

      CEGUI::LuaScriptModule& scriptmod(CEGUI::LuaScriptModule::create());
      m_pLuaState = scriptmod.getLuaState();
      m_pGUISystem->setScriptingModule(&scriptmod);

and I also have a LuaScriptManager which I use it for get the global datas from lua and explosing C++ class to lua.
I also want the CEGUI lua feature which is easy to code GUI Event in lua script.
so I pass the lua_state from CEGUI to LuaScriptManager, like this:

Code: Select all

   IMPLEMENT_SINGLETON(LuaScriptManager)
   //--------------------------------------------------------------------------------------------------------
   LuaScriptManager::LuaScriptManager()
   {
      m_pLuaState = UISystem::GetSingleton().GetLuaState();
      luaL_openlibs(m_pLuaState);
   }
   //--------------------------------------------------------------------------------------------------------
   LuaScriptManager::~LuaScriptManager()
   {
      lua_close(m_pLuaState);
   }


The problem is : when I call the LuaScriptManager:DestructSingleton() which calls the ~LuaScriptManager() the program crash and a "debug assertion failed" dialog appeared. So I was wondering if something going wrong in the lua_state* pointer. Does CEGUI destroy the lua_state automatically so I just can't use it after it's done?
If I don't LuaScriptManager's destructor it works fine, and the CEGUI log says:

Code: Select all

---- Destroying Lua bindings ----

Does it mean lua_state* is closed?
Appreciate any advice! :)

pizzazhang
Not too shy to talk
Not too shy to talk
Posts: 23
Joined: Wed Apr 13, 2011 14:54
Location: Shanghai/China

Re: Does CEGUI Destroy the lua_state automatically?

Postby pizzazhang » Sun Jun 05, 2011 04:47

I looked into the CEGUI source, and found that if I didn't pass a lua_state* to CEGUI it will not destroy for me.
I changed my code:

Code: Select all

LuaScriptManager::LuaScriptManager()
   {
      m_pLuaState = luaL_newstate();
      luaL_openlibs(m_pLuaState);
   }
   //--------------------------------------------------------------------------------------------------------
   LuaScriptManager::~LuaScriptManager()
   {
      lua_close(m_pLuaState);
   }

and pass the custom lua_state* to CEGUI:

Code: Select all

      CEGUI::LuaScriptModule* scriptmod = &CEGUI::LuaScriptModule::create(LuaScriptManager::GetSingleton().GetLuaState());
      m_pGUISystem->setScriptingModule(scriptmod);

when I the LuaScriptManager destructed, the program still crashed. If I comment the code above, it works fine.
but I really need the CEGUI bindings to lua, is there any way to solve this problem?

pizzazhang
Not too shy to talk
Not too shy to talk
Posts: 23
Joined: Wed Apr 13, 2011 14:54
Location: Shanghai/China

Re: Does CEGUI Destroy the lua_state automatically?

Postby pizzazhang » Sun Jun 05, 2011 08:32

well, I decide to use tolua++ to bind my class and just use the CEGUI's own lua_state. But I have a problem, in my code I define a macro that will add Singleton code to my class:

Code: Select all

   class UICreditState :  public UIState
   {
      DECLARE_SINGLETON(UICreditState)

I don't know how to export the code which the macro has in tolua++.

pizzazhang
Not too shy to talk
Not too shy to talk
Posts: 23
Joined: Wed Apr 13, 2011 14:54
Location: Shanghai/China

Re: Does CEGUI Destroy the lua_state automatically?

Postby pizzazhang » Mon Jun 06, 2011 02:58

well, when I can't solve the problem in one way, I choose another way. Now I use the template Singleton class to implement the singleton and now it works fine... BTW tolua++ is really simple and easy to use (luabind just sucks, I need to write all the parent singleton class and I just don't use them...) :wink:

User avatar
CrazyEddie
CEGUI Project Lead
Posts: 6760
Joined: Wed Jan 12, 2005 12:06
Location: England
Contact:

Re: Does CEGUI Destroy the lua_state automatically?

Postby CrazyEddie » Mon Jun 06, 2011 07:33

Yeah, I think tolua++ is a nice solution.

Years ago the very early tests of CEGUI's lua integration were done with luabind. It worked ok, though compilation would kill the compiler sometimes! I think the one thing that luabind allowed that is sadly missing in tolua++ is the ability to create subclasses in scripts - it was a mighty feeling to be able to create and register new CEGUI window types in lua :D

CE


Return to “Help”

Who is online

Users browsing this forum: No registered users and 3 guests