Custom Lua allocation function's freeing throws COMException

  • From: Mars Arkane <marsarkane@xxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Sun, 12 Feb 2017 22:54:53 +0100

Hi everyone.

I'm using LuaJit with C# and I'm trying to use a custom allocation function
to control the amount of memory used by Lua, but for some reason I get a
COMException while Lua is trying to free memory, saying that an argument is
invalid (HRESULT: 0x80000003), if I let a script that creates and extends
strings run for a few seconds. Here's my custom allocation function:

private static IntPtr Alloc(IntPtr ud, IntPtr ptr, int osize, int nsize)
{
    if (nsize == 0)
    {
        Marshal.FreeHGlobal(ptr);
        return IntPtr.Zero;
    }
    else
    {
        if (osize == 0)
            return Marshal.AllocHGlobal(nsize);
        else
            return Marshal.ReAllocHGlobal(ptr, (IntPtr)nsize);
    }
}

There's nothing special going on, I simply set that function with
lua_setallocf and then start to create and concat strings in a loop, to
cause memory changes. If I don't set function everything works fine, just
as it does if I use the normal Lua 5.1.

Could this be a bug in LuaJit? I haven't tested it in C yet.

-- Mars

Other related posts: