Unexpected garbage collection
pakozm opened this issue · 0 comments
pakozm commented
A problem exists when Lua garbage collector deletes a variable which has a reference in C++ but not in Lua. The following example shows a code which could end with a segmentation fault because of this issue:
function blah(db)
local gridfs = mongo.GridFS.New(db, "foo")
local file = gridfs:find_file("bar")
return function()
-- do whatever with file
print(file:chunk(0):data())
end
end
This code will produce arbitrary segmentation faults depending in the Lua garbage collection. The reference to gridfs
only exists as local in the function blah
. The returned closure captures the local file
but not gridfs
. So, at the end Lua garbage collector will delete gridfs
variable because it is not referenced. Unfortunatelly, the GridFile
C++ object contains a reference to this variable, but Lua is not acknowledged of that, and the call to file:chunk(0)
will produce a segmentation fault.