Re: cdata finalizer called twice

  • From: Peter Colberg <peter@xxxxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Sun, 16 Nov 2014 16:54:24 -0500

On Sun, Nov 16, 2014 at 02:03:05AM -0500, Peter Colberg wrote:
> Finally I discovered why some cdata objects were finalized twice.

Does the following function return a reference to an unanchored cdata?

  local uint64_t = ffi.typeof("uint64_t")
  local uint64_t_1 = ffi.typeof("uint64_t[1]")

  local function get_event_profiling_info_start(event)
    local value = uint64_t_1()
    assert(0 == C.clGetEventProfilingInfo(event, C.CL_PROFILING_COMMAND_START, 
ffi.sizeof(uint64_t), value, nil))
    return value[0]
  end

If I change these functions to explicitly make a copy,

  local function get_event_profiling_info_start(event)
    local value = uint64_t_1()
    assert(0 == C.clGetEventProfilingInfo(event, C.CL_PROFILING_COMMAND_START, 
ffi.sizeof(uint64_t), value, nil))
    return uint64_t(value[0])
  end

the duplicate finalization is resolved, too.

Consider the following example:

  local ffi = require("ffi")
  local count = 0
  local buf = ffi.new("uint64_t[1]")
  local function inc()
    count = count + 1
    buf[0] = count
    return buf[0]
  end

  local v1 = inc()
  local v2 = inc()
  local v3 = inc()
  print(v1, v2, v3)     -- 1ULL    2ULL    3ULL

Why does that not print '3ULL 3ULL 3ULL', if buf[0] is a reference cdata?

Thanks,
Peter

Other related posts: