Calling C dll from love2d
Posted: Sun Feb 02, 2020 10:29 pm
I have a "it works fine in Linux, but fails in Windows" error that I think is really a "it happens to work in Linux, but never in Windows" issue.
Namely, I have library code for our game built into a .so/.dll using gcc (for both Linux and Windows.) Unfortunately, when I run in Windows I get a segfault when I attempt to register my C functions. Here is the first line in my main.lua:
In my .so/.dll I have:
Naturally lslib is setup with the correct table of functions. The debugger tells me that the seg fault occurs on 'luaL_register'. I put printf in there to confirm. If I print out lua_State (the L), I get a non-null pointer. Again Linux works without issue...
From what I researched on the internet, I've noticed that if threads are used (which gdb reports Love2d is using) then the passed in lua_State may have a race condition with the garbage collection. A solution listed is to use only the global lua state when Lua was instantiated. Unfortunately, I don't know how to get it. Although, I don't know how this would happen as the thread should still be open...
Is there a love2d C function to get the global lua state? Maybe in a Love2d .dll? I noticed in lua 5.2 that there is a function call to get this, but we have 5.1.
Also, is there a way to run love2d in a single thread? It is annoying running a debugger and/or Valgrind on love2d...
Namely, I have library code for our game built into a .so/.dll using gcc (for both Linux and Windows.) Unfortunately, when I run in Windows I get a segfault when I attempt to register my C functions. Here is the first line in my main.lua:
Code: Select all
-- This loads the C library and binds is to a 'ds' object
ds = require 'libds'
Code: Select all
// How Lua registers the functions.
int luaopen_libds (lua_State *L){
printf("Registering functions...\n");
fflush(stdout);
luaL_register(L, "libds", lslib);
printf("Functions Registered.\n");
fflush(stdout);
return 1;
}
From what I researched on the internet, I've noticed that if threads are used (which gdb reports Love2d is using) then the passed in lua_State may have a race condition with the garbage collection. A solution listed is to use only the global lua state when Lua was instantiated. Unfortunately, I don't know how to get it. Although, I don't know how this would happen as the thread should still be open...
Is there a love2d C function to get the global lua state? Maybe in a Love2d .dll? I noticed in lua 5.2 that there is a function call to get this, but we have 5.1.
Also, is there a way to run love2d in a single thread? It is annoying running a debugger and/or Valgrind on love2d...