lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


   first let me explain a bit our situation :

    We are developing a C++ modular and cross platform framework, used to create games, serious games, smart-phone apps...
    Based on this framework, we have build a tool (running on Windows OS) to edit the contents of applications build on our framework (set up IHM, navigation...).
    The tool is itself a module so it's loaded by the application as a dynamic library (briefly : it's a plug-in, and can be loaded at runtime).

    We also have a LUA module in our framework.
    The LUA module can be used in the application or not. If it is, LUA is statically linked to the application.
    The LUA module is used by the Tool module (lua is statically linked to the tool DLL). The Tool detect if LUA module is already init by the application and if not init its own.

     So if an application use LUA, we have crash when running lua code on the tool side because of the static Node dummynode_  struct init in both application memory space and tool dll memory space.

     I have modified ltable.c and .h a little for a workaround for our purpose like that :

    ltable.h added this code :

#ifdef __cplusplus
extern "C" Node* externdummynode_;
extern Node* externdummynode_;

    ltable.cpp changed some code :

Node* externdummynode_ = 0;

#define dummynode        (externdummynode_)

and removed static dummynode_

    in our LUA module code initialisation, we create (with a new) and init externdummynode_ and keep a reference on it, and in our Tool dll, we init externdummynode_ again (the dll memory space variable this time) with something like that :

externdummynode_ = (Node*)m_lua_module->getLUADummyNode();

The workaround works for us, but it would be really nice to have a more clean mechanism integrated to lua code. Perhaps creating externdummynode_ if not already init in lua_newstate, store a copy of  externdummynode_ in the lua_State and have a macro like this :
#define lua_dynamic_reinit(state
) externdummynode_ = state->dummynode_;

thanks for your comments and feedback (and perhaps changes in lua code ;-) )

   Stéphane Capo