11.8 freeaddrinfo FunctionAll the storage returned by getaddrinfo , the addrinfo structures, the ai_addr structures, and the ai_canonname string are obtained dynamically (e.g., from malloc ). This storage is returned by calling freeaddrinfo .
ai should point to the first addrinfo structure returned by getaddrinfo . All the structures in the linked list are freed, along with any dynamic storage pointed to by those structures (e.g., socket address structures and canonical hostnames). Assume we call getaddrinfo , traverse the linked list of addrinfo structures, and find the desired structure. If we then try to save a copy of the information by copying just the addrinfo structure and calling freeaddrinfo , we have a lurking bug. The reason is that the addrinfo structure itself points to dynamically allocated memory (for the socket address structure and possibly the canonical name ), and memory pointed to by our saved structure is returned to the system when freeaddrinfo is called and can be used for something else.
|