Sharing a lua_State across DLL boundary

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Sharing a lua_State across DLL boundary

Julien Lebot-2
Hi all,

I have a DLL which opens a lua_State and calls luabind::open on it. I
then expose a few classes to it, and all is
working well.
Now I have a client executable that links with this DLL and where I'd
like to expose some more classes, so I obtain the lua_State created in
this DLL, bind a few more classes to it.
The problem is when I try to call functions from Lua on those classes
lua/luabind crashes, either with "lua runtime error" if I did
luabind::call_function and the lua function calls my new C++ classes'
functions or something like "No matching overload found, candidates:
void loadResource(ResourceManager&, std::string const&)" if I use
lua_dofile and the file calls my new C++ classes' functions.

Any idea ?

Regards,
Julien Lebot

------------------------------------------------------------------------------
WhatsUp Gold - Download Free Network Management Software
The most intuitive, comprehensive, and cost-effective network
management toolset available today.  Delivers lowest initial
acquisition cost and overall TCO of any competing solution.
http://p.sf.net/sfu/whatsupgold-sd
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: Sharing a lua_State across DLL boundary

Jason McKesson
On Apr 26, 2011 9:57 PM, Julien Lebot wrote:

> Hi all,
>
> I have a DLL which opens a lua_State and calls luabind::open on it. I
> then expose a few classes to it, and all is
> working well.
> Now I have a client executable that links with this DLL and where I'd
> like to expose some more classes, so I obtain the lua_State created in
> this DLL, bind a few more classes to it.
> The problem is when I try to call functions from Lua on those classes
> lua/luabind crashes, either with "lua runtime error" if I did
> luabind::call_function and the lua function calls my new C++ classes'
> functions or something like "No matching overload found, candidates:
> void loadResource(ResourceManager&, std::string const&)" if I use
> lua_dofile and the file calls my new C++ classes' functions.
>
> Any idea ?
>
> Regards,
> Julien Lebot
In order to do what you're suggesting, both Lua and Luabind must be
built as DLL's themselves, and all of the DLL's using them must be
linked against those DLL's.

------------------------------------------------------------------------------
WhatsUp Gold - Download Free Network Management Software
The most intuitive, comprehensive, and cost-effective network
management toolset available today.  Delivers lowest initial
acquisition cost and overall TCO of any competing solution.
http://p.sf.net/sfu/whatsupgold-sd
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: Sharing a lua_State across DLL boundary

Peter Colberg-3
In reply to this post by Julien Lebot-2
Hi Julien,

not sure if this applies to Windows and DLLs, too, but I had a similar
experience with GCC and shared modules on Linux: classes registered
with Luabind where only valid within that shared library, but caused
segmentation faults if used across shared library boundaries.

The solution was to patch the luabind::type_id class, and compare
using typeid(T).name() instead of typeid(T)::operator=. For GCC, the
reason why the typeid operator might not work across shared libraries
is explained here [1]. In this particular case, I loaded the shared
library with Lua's require(), which, unfortunately, does not pass
RTLD_GLOBAL to dlopen.

[1] http://gcc.gnu.org/faq.html#dso

The typeid equality problem has appeared in other C++ libraries, e.g.
in boost::any[2], with the same fix[3], typeid(T).name() comparison.

[2] https://svn.boost.org/trac/boost/ticket/754
[3] https://svn.boost.org/trac/boost/changeset/56168

Maybe the attached patch helps in the case of DLLs, too.

Peter

------------------------------------------------------------------------------
WhatsUp Gold - Download Free Network Management Software
The most intuitive, comprehensive, and cost-effective network
management toolset available today.  Delivers lowest initial
acquisition cost and overall TCO of any competing solution.
http://p.sf.net/sfu/whatsupgold-sd
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user

luabind_type_id_across_shared_libraries.patch (817 bytes) Download Attachment