Skip to content

Unexpected garbage collection #29

@pakozm

Description

@pakozm

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions