Bug#318317: libc6: Numerous (49) memory leaks in gethostbyname, as reported by mudflap
On Thu, Jul 14, 2005 at 10:17:32PM +0300, Vesselin Peev wrote:
> Could you please elucidate what is this programming pattern that causes the
> leaks, if it is possible to do with a programming snippet? I find it very
> strange that a well-working program will have leaks that are considered
> necessary because of a quite common usage pattern. Isn't there a better,
> more perfect way?
Very simple: gethostbyname() returns a "struct hostent *" for which the
C library has to allocate some internal memory. However,
POSIX/SUS/etc. does not define any API to tell the C library that the
returned pointer is no longer needed, so the allocated memory cannot be
freed by the C library until the next call to gethostbyname().
Solution: do not use the gethostby* functions. Use get{addr|name}info()
instead, they do not have this API problem (and have other advantages
as well).
Gabor
--
---------------------------------------------------------
MTA SZTAKI Computer and Automation Research Institute
Hungarian Academy of Sciences
---------------------------------------------------------
Reply to: