[1.1.32]Reference counting in lua memory managing doesn't function properly.
Posted: Tue Apr 20, 2021 1:53 pm
I also post this to bug report.
Also, I would ask, if this is correct according to the memory mechanism in lua. I remember the referencing mechanism is something called reference counting. If variable a stores a table, then b = a, after this assignment, a still store the pointer to the table.
But in this case, a nearly equals nil.
This trouble makes it even harder to write pseudo-OOP code.
Any idea? Thanks.
The global.mt.test_array should always store the add function inside it.
But the log is very clear.
No idea what happened. The = operator doesn't function in the reference counting way. Is this correct in lua?
Also, I would ask, if this is correct according to the memory mechanism in lua. I remember the referencing mechanism is something called reference counting. If variable a stores a table, then b = a, after this assignment, a still store the pointer to the table.
But in this case, a nearly equals nil.
This trouble makes it even harder to write pseudo-OOP code.
Any idea? Thanks.
Code: Select all
global = global or {}
global.mt = global.mt or {}
global.mt.test_array = {}
global.mt.test_array.what = "test_array"
global.mt.test_array.add = function(this, element)
this.length = this.length +1
rawset(this, this.length, element)
end
-------------- CTOR n DTOR ----------------
global.new = global.new or {}
global.new.test_array = function()
local this = {}
this.length = 0
this.mt = global.mt.test_array
return this
end
---------------------------------------
global.cont = global.new.test_array()
script.on_event(defines.events.on_tick, function(event)
if game.tick == 1
then
global.cont.mt.add(global.cont, "11111111111")
end
if game.tick == 3
then
log(serpent.block(global))---------- Calls the log function.
end
end)
But the log is very clear.
Code: Select all
Factorio_1.1.32/temp/currently-playing/control.lua:54: {
cont = {
"11111111111",
length = 1,
mt = {
add = ((loadstring or load)("\27LuaR\0\1\4\8\4\8\0\25揬13\n\26\n\19\0\0\0\22\0\0\0\2\0\6\9\0\0\0嘰0@\0岪@\1\n€\0€唨@\0繺0\0\0\7\1@\0@\1€\0滰\0\2\31\0€\0\3\0\0\0\4\7\0\0\0\0\0\0\0length\0\3\0\0\0\0\0\0?\4\7\0\0\0\0\0\0\0rawset\0\0\0\0\0\1\0\0\0\0\0L\0\0\0\0\0\0\0@D:/factorio stand alone/Factorio_1.1.32/temp/currently-playing/control.lua\0\9\0\0\0\20\0\0\0\20\0\0\0\20\0\0\0\21\0\0\0\21\0\0\0\21\0\0\0\21\0\0\0\21\0\0\0\22\0\0\0\2\0\0\0\5\0\0\0\0\0\0\0this\0\0\0\0\0\9\0\0\0\8\0\0\0\0\0\0\0element\0\0\0\0\0\9\0\0\0\1\0\0\0\5\0\0\0\0\0\0\0_ENV\0",'@serialized')),
what = "test_array"
}
},
mt = {----------------------this table shouldn't be removed.
test_array = 0
},
new = {
test_array = ((loadstring or load)("\27LuaR\0\1\4\8\4\8\0\25揬13\n\26\n\26\0\0\0\31\0\0\0\0\0\2\8\0\0\0\11\0\0\0\n@@€F繞\0G€繺0G\0羂0\n@\0乗31\0\0\1\31\0€\0\5\0\0\0\4\7\0\0\0\0\0\0\0length\0\3\0\0\0\0\0\0\0\0\4\3\0\0\0\0\0\0\0mt\0\4\7\0\0\0\0\0\0\0global\0\4\11\0\0\0\0\0\0\0test_array\0\0\0\0\0\1\0\0\0\0\0L\0\0\0\0\0\0\0@D:/factorio stand alone/Factorio_1.1.32/temp/currently-playing/control.lua\0\8\0\0\0\27\0\0\0\28\0\0\0\29\0\0\0\29\0\0\0\29\0\0\0\29\0\0\0\30\0\0\0\31\0\0\0\1\0\0\0\5\0\0\0\0\0\0\0this\0\1\0\0\0\8\0\0\0\1\0\0\0\5\0\0\0\0\0\0\0_ENV\0",'@serialized'))
}
}