views:

244

answers:

4

Hello,

I am writing client for TCP connection and conversion from IP to socket_addr makes memory leaks.

There is following process:

#include <netdb.h>
#include <sys/socket.h>
#include <sys/types.h>

/** there is some code like method header etc. */

hostent * host = gethostbyaddr( ip, 4, AF_INET ); // ip is char[4], I use IPv4

if ( !host ) return -2; // bad IP

netSocket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
if ( netSocket == -1 ) return -3; // error during socket opening

sockaddr_in serverSock;
serverSock.sin_family = AF_INET;
serverSock.sin_port = htons( port );
memcpy( &( serverSock.sin_addr ), host->h_addr, host->h_length );

// and now there is function connect(...);

/** end of method */

This code works fine but when I tracked memory using I took 5 memory leaks. They are created by this line:

hostent * host = gethostbyaddr( ip, 4, AF_INET ); // ip is char[4], I use IPv4

I have tried delete it delete host; but this causes segmentation fault.

Do you have any ideas how I can clean the memory, please? This is my school project and we have to work with memory correctly.

EDIT: I am using Linux Ubuntu 9.04, g++ 4.3.3 and for memory testing mudflap library

+6  A: 

You don't say what platform you are on, but typically the memory returned by gethostbyaddr will be allocated and managed by the sockets library you are using - you don't free it yourself. Whatever you are using to diagnose leaks is probably giveing false positives.

For example, this man page http://www.opengroup.org/onlinepubs/009695399/functions/gethostbyaddr.html says that the pointer returned may be to static data, while MS use thread local storage. In neither case can or should the data be freed, and in neither case is there a leak.

anon
I am using Linux Ubuntu 9.04 with g++ compiler 4.3.3. I test memory with mudflap and it works pretty good so I don't think that it is false positive
Gaim
All memory allocatotion trackers that I have ever used give false positives. Anyway, if you want your application to be at all portable, there is no way of freeing the memory.
anon
Thanks for your solution although it didn't delight me.
Gaim
+1  A: 

Remarks from the Windows gethostbyaddr function remarks at MSDN:

An application should not try to release the memory used by the returned hostent structure. The application must never attempt to modify this structure or to free any of its components. Furthermore, only one copy of this structure is allocated per thread, so the application should copy any information it needs before issuing any other function calls to gethostbyaddr or gethostbyname.

schnaader
Thanks for information but it doesn't solve my problem with leaks
Gaim
+1  A: 

The hostent structure is allocated internally by the socket library and retained for reuse by subsequent calls. The memory is not being leaked even though, as you observed, it is not being released.

William Bell
I am using Unix sockets so it isn't WinSock - it is in the topic name
Gaim
+1  A: 

Try using getaddrinfo() and freeaddrinfo() as suggested by the manpage for gethostbyname.

Andrew
Ok, I will try it, thanks. Can you give me a manpage link please? I have tried to find it but without success.
Gaim
Dewayne Christensen