I am having some memory leaking troubles in an embedded application and while looking in the code, I see I do not freeaddrinfo()
when getaddrinfo()
returns non-zero:
s = getaddrinfo(hostname, port, &hints, &result);
if (s != 0) {
log_error();
} else {
// do stuff
freeaddrinfo(result);
}
Could this lead to memory leaks? I have tried to look into the man page, but it doesn't state it explicitly.
The specification doesn't say that result
isn't assigned if it fails, so it appears that a conforming implementation could do so.
Why not free it unconditionally?
result = 0;
s = getaddrinfo(hostname, port, &hints, &result);
if (s) {
// log error
} else {
// success
}
if (result) {
freeaddrinfo(result);
}
Ideally you'd be able to call freeaddrinfo(result)
without checking whether result
is NULL, but although it's implied by the standard that that's OK I wouldn't count on it.