I use github.com/nim-lang/lua to make a high level bind to lua I bind a nim object type to lua, and I wanna use testtype.new() to get a userdata Here is the code: import lua var L = newState() L.openlibs() type test2 = ref object a :int b :string fuck = object ud:test2 L.newtable() L.pushstring("new") L.pushcfunction(proc(l:PState):cint{.cdecl.}= var p = cast[ptr fuck](L.newuserdata(sizeof(fuck).cint)) echo repr(p) p.ud = new(test2) #GC_ref(p.ud) return 1) L.settable(-3) L.setGlobal("testtype") var res = L.dofile("a.lua") if res > 0: echo "testfail:" & L.tostring(-1) L.close()
and a.lua is: print(11) print(11) print(11) print(11) print(11) print(11) print(11) print(testtype.new()) print(testtype.new()) print(testtype.new()) print(testtype.new()) print("==========") when i run the program, it just crashed: 11 11 11 11 11 11 11 Traceback (most recent call last) a.nim(22) a lua.nim(593) dofile a.nim(15) :anonymous repr.nim(308) reprAny repr.nim(252) reprAux repr.nim(234) reprRef repr.nim(245) reprAux repr.nim(212) reprRecord repr.nim(195) reprRecordAux repr.nim(252) reprAux repr.nim(234) reprRef repr.nim(245) reprAux repr.nim(212) reprRecord repr.nim(199) reprRecordAux repr.nim(195) reprRecordAux repr.nim(255) reprAux SIGSEGV: Illegal storage access. (Attempt to read from nil?) ,but if i remove the print(11) above, the result is correct: ref 0x1a5dc98 --> [ud = nil] userdata: 0x1a5dc98 ref 0x1a5dd78 --> [ud = nil] userdata: 0x1a5dd78 ref 0x1a5de28 --> [ud = nil] userdata: 0x1a5de28 ref 0x1a5e358 --> [ud = nil] userdata: 0x1a5e358 ========== \---- So, I guess it's maybe the gc problem, but no matter how i test, it just crash,So does anyone konw where the problem is?