Skip to content

Commit

Permalink
Fix memory leak issue (miekg#9)
Browse files Browse the repository at this point in the history
Replaced the defer C.ub_resolve_free with two direct calls to
C.ub_resolve free; it appears that defer is not always working correctly
in the current version of Go (1.6.1)
  • Loading branch information
tjeb authored and miekg committed Jun 15, 2016
1 parent c0c307b commit 408511a
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion unbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,12 +206,16 @@ func (u *Unbound) Resolve(name string, rrtype, rrclass uint16) (*Result, error)
defer C.free(unsafe.Pointer(cname))
res := C.new_ub_result()
r := new(Result)
defer C.ub_resolve_free(res)
// Normally, we would call 'defer C.ub_resolve_free(res)' here, but
// that does not work (in Go 1.6.1), see
// https://github.com/miekg/unbound/issues/8
// This is likely related to https://github.com/golang/go/issues/15921
t := time.Now()
i := C.ub_resolve(u.ctx, cname, C.int(rrtype), C.int(rrclass), &res)
r.Rtt = time.Since(t)
err := newError(int(i))
if err != nil {
C.ub_resolve_free(res)
return nil, err
}

Expand Down Expand Up @@ -274,6 +278,7 @@ func (u *Unbound) Resolve(name string, rrtype, rrclass uint16) (*Result, error)
b = C.GoBytes(unsafe.Pointer(C.array_elem_char(res.data, C.int(j))), C.array_elem_int(res.len, C.int(j)))
}
}
C.ub_resolve_free(res)
return r, err
}

Expand Down

0 comments on commit 408511a

Please sign in to comment.