tilkinsc/Lua.NET

Debug hooks not working properly

Closed this issue · 4 comments

TGMM commented

I'm getting "System.AccessViolationException: Attempted to read or write protected memory" when trying to use the Debug library hooks.
I've tried with all the Lua versions and none of them seem to work, here's a snippet of code that will help reproduce the issue.

using LuaNET.Lua54;
using NativeMethods = LuaNET.Lua54.Lua;

var lua = NativeMethods.luaL_newstate();
NativeMethods.luaL_openlibs(lua);
NativeMethods.lua_sethook(lua, Hook, NativeMethods.LUA_MASKLINE, 0);
NativeMethods.luaL_dostring(lua, "print(10)\nprint(20)\nlocal x = 10");
return;

static void Hook(lua_State state, NativeMethods.lua_Debug func)
{
    Console.WriteLine("Test");
}

The function signature in lua.h does seem to match the bindings, so not sure what could be wrong here.

It fails because null of a struct (lua_KContext) is Nullable<lua_KContext>, so basically you have the nullable object instead of the struct.

It is also cuz lua_Debug has strings and C# isn't marshalling the strings, just exploding

Please pull the branch lua-debug-fix and play around with it. I just changed the strings to nints, so inconvenient, yes, but it works.