Hello to the list.

I am a new user of LCLint and fairly new to programming in a Linux
environment.  I am currently developing code under Red Hat 7.0 using gcc.
I've managed to work most things out, but one message from LCLint has me
stumped, and I can't tell if it's something I need to adjust in my code or
something I need to tell LCLint.

In my socket code I am performing a couple of calls to gethostbyname() in
the form pstrHostEnt = gethostbyname(pszLocalHost). LCLint complains:

socket.c:91:18: Fresh storage pstrHostEnt not released before return
   A memory leak has been detected.  Newly-allocated or only-qualified
storage is
   not released before the last reference to it is lost. (-mustfree will
   supress message)
      socket.c:76:7: Fresh storage pstrHostEnt allocated

This indicates to me that LCLint believes gethostbyname() is allocating a
hostent structure and I'm not freeing it.  (And I'm not; I've never seen
memory freed after a gethostbyname() call, and I didn't think it was
necessary.) I confirmed this by tossing in a free(pstrHostEnt) -- even
though I'm highly doubtful that this is what I should be using -- and the
warning went away.

As far as I knew, gethostbyname() used a static structure and no
deallocation was necessary.  But I checked the netdb.h include file and
struct hostent is NOT shown as static.  So I did some digging and, on a
Linux-Net list archive, found the statement that "newer" versions of
gethostbyname() malloc the hostent structure and that one should use
freehostent() to free the memory.  A recursive grep of /usr/include,
however, turns up no such function.

So, I am left wondering if LCLint is incorrectly nagging me when I don't
need to do anything other than somehow correct LCLint's perception of
reality (which I thought at first, but now question), or if gethostbyname()
really is allocating memory and I should be using freehostent() and can't
find/don't have it, or if it's malloc-ing memory and free() is actually the
beast to use.

I was going to look at the library source to try to noodle this out, but
I'm running on a "publisher's version" of RH 7.0 which apparently doesn't
include source. <Sigh>  I'm casting about now to see if I can track down
what I need, but I thought I'd post here as well to see if anyone can offer
any pointers.  Keep in mind that for a Linux/LCLint newbie like myself,
even the simplest things can be monumental, so *any* help or suggestions
anyone can offer would be greatly appreciated.

Thanks,
Rick


Reply via email to