lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Fri, Dec 2, 2011 at 12:01 PM, Justin Cormack
<justin@specialbusservice.com> wrote:

> If you add collectgarbage() after the request stage and before get replies
> you get a free error every time - storing a pointer to a Lua reference in a
> C struct does not stop Lua freeing it... You allocate host2.ar_name for
> example as a copy of query (you can just use query directly here) and so
> that gets garbage collected.

Many thanks. I fixed the first part by:

1) anchoring the calloc pointer with talloc[#talloc+1] = host
2) host2.ar_name = tquery[i]

I get a segmentation fault in the second part as well. I tried either
freeing the calloc ptr within the loop (as I get the replies) or at
the end:

for _,p in ipairs(talloc) do if p ~= nil then C.free(p) end end

-----------------------------------------------------------------------------

local fdsi  = ffi.new("struct signalfd_siginfo[1]")
local fdsip = sfd_si_tp(fdsi)

-- get replies
while true do
        if nqueries == nresolved then break end

        local s = C.read(sfd, fdsi, ffi.sizeof(sfd_si_t))
        nresolved = nresolved + 1

        if s ~= ffi.sizeof(sfd_si_t) then print("error: read:", s) break end
        if fdsip.ssi_signo ~= SIGRTMIN then print("received signal:",
fdsip.ssi_signo) break end

        local outp = ffi.cast("struct gaicb**", fdsip.ssi_ptr)
        if not ffi.cast("struct addrinfo*", outp[0].ar_result) then
                print("error: cannot resolve:",
ffi.string(ffi.cast("const char*", outp[0].ar_name)))
        end

        -- ai_addr: struct sockaddr*
        local ap = ffi.cast("struct sockaddr_in*", outp[0].ar_result.ai_addr)
        local name = outp[0].ar_name
        local ip   = C.inet_ntoa(ap.sin_addr)
        print(ffi.string(name), ffi.string(ip)) io.flush()

        --if outp[0] ~= nil then C.free(outp[0]) end
end

for _,p in ipairs(talloc) do if p ~= nil then C.free(p) end end