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
resultisn’t assigned if it fails, so it appears that a conforming implementation could do so.Why not free it unconditionally?
Ideally you’d be able to call
freeaddrinfo(result)without checking whetherresultis NULL, but although it’s implied by the standard that that’s OK I wouldn’t count on it.