lua-gdextension icon indicating copy to clipboard operation
lua-gdextension copied to clipboard

Crashes when converting tables to Variant

Open gilzoide opened this issue 6 months ago • 6 comments

Hi! I’m new to Godot and still learning how to use lua-gdextension. While experimenting, I noticed some strange crashes and I’m not sure if this is expected behavior or a bug.

local player = {
    extends = Node2D,
}

-- Case 1: Lua tables inside Array
function player:test1()
    local array1 = {}
    local array2 = {}
    return Array{array1, array2} -- segfault
end

-- Case 2: Multiple return values
function player:test2()
    return {}, 1 -- segfault
end

function player:_process(dt)
    self:test1()
    self:test2()
end

return player

What I observed return Array{{}} → segfault return {}, 1 → segfault return {} → does not crash return Array{1, 2, 3} → works fine

Is this the correct/expected behavior (Lua tables not allowed, only Godot-native types)? Or should this raise an error instead of crashing?

Originally posted by @modcos in https://github.com/gilzoide/lua-gdextension/discussions/153

gilzoide avatar Sep 03 '25 21:09 gilzoide

Thanks for the clarification.

I tested both 0.5.0 builds:

lua-gdextension+luajit.zip → segfaults with the examples above. lua-gdextension.zip → works fine with the examples, but I occasionally see errors in the debugger at unpredictable times.

modcos avatar Sep 03 '25 23:09 modcos

Couldn't replicate from latest commit gilzoide/lua-gdextension@bb3b11f, likely fixed already.

Denneisk avatar Sep 04 '25 03:09 Denneisk

@modcos check out the guide in #15 and downloaded the latest build and report whether it still happens.

Denneisk avatar Sep 04 '25 03:09 Denneisk

I tested again with the latest build. Downloaded lua-gdextension+luajit from Actions: link.

Minimal project setup: just a scene with a Node and the script attached (same example from the topic post).

  • On the +luajit build, I consistently get segfaults within 1–5 seconds after running.
  • On the non-luajit build, the examples usually work, but if I duplicate the test1() and test2() calls many times in _process (without a loop, just repeated lines, ~20+ calls each), I also get segfaults.

Example:

function player:_process(dt)
    self:test1()
    self:test1()
    ...
    self:test2()
    self:test2()
end

Environment:

  • Godot version: v4.4.1.stable.official [49a5bc7b6]
  • Commit tested: bb3b11f
  • OS: Windows 10

So it seems the issue is easier to reproduce on the +luajit build, but can also happen on the plain version when the functions are called enough times.

modcos avatar Sep 04 '25 10:09 modcos

Oh, my bad. I guess I wasn't running it long enough.

Denneisk avatar Sep 04 '25 12:09 Denneisk

Hey folks, thanks for the additional info. I'll check it out at some point. Maybe memory allocations fail at some point and it crashes because of that. We could add an error log whenever Lua allocator function fails and check if that's the case.

gilzoide avatar Sep 04 '25 12:09 gilzoide