This code does not leak. Either the leak detection is faulty, or the code that you are actually executing is more complicated than this, and the leak is in the code that you did not show.
The only memory allocated by Delphi RTL in the code in the question is for dynamic strings. Delphi dynamic line processing does not proceed. WinSock, gethostbyname
and inet_ntoa
allocate internal memory for WinSock.
In the case of gethostbyname
:
The hostent memory returned by gethostbyname is allocated inside the Winsock DLL from the local thread store. Only one host structure is allocated and used, regardless of how many times the gethostbyaddr or gethostbyname functions are called in the stream. The returned host structure must be copied to the application buffer if additional calls must be made in the gethostbyname or gethostbyaddr functions in the same thread. Otherwise, the return value will be overwritten by subsequent calls to gethostbyname or gethostbyaddr in the same stream. The internal memory allocated for the returned host structure is issued by the Winsock DLL when the stream exits.
And also for inet_ntoa
:
The string returned by inet_ntoa is in memory allocated by Windows sockets. An application should not make any assumptions about how memory is allocated. The returned string is guaranteed to be valid only until the next call to the Windows Sockets function is made within the same thread.
Although it is true that the code in the question does not call WSACleanup
, this is fine, since it is pretty pointless to return resources in the process at the end time.
David heffernan
source share