FreeRTOS TCP/IP Stack possible memory leak?

@Stonebull wrote:

Could you verify with your server if there is a memory loss on your system?

I did verify that, and I did not notice any loss of memory. I connected and disconnected telnet several times.

Thanks for your example altough for now I would prefer to keep using my current implementation. I’m explaining why I’ve chosen my approach in the reply post to @kanherea if you’re interrested.

Yet, I would recommend to study well the telnet source well, it explains a lot.

At first I implemented a server that allows only one client and re-uses the listener socket

That indeed will not work. You will have to call listen with a value of 2 or more for xBacklog.

I wrote:

Normally, a TCP server will not shut down a connection. It is the client who initiates a graceful shutdown.

How am I supposed to kick a client from server side?

In that case you can start a shutdown as I have described in my post on the FreeRTOS forum, summarised as shutdown, wait and try to read from socket, close socket.

I just want to allow one client at a time to be connected, and always the latest that attempts to connect…

All right, in that case, you will have to follow the procedure shutdown/wait/closesocket, to kill the older connection.

Is it advisable to use shutdown on server side at all then? Or is it also “common practice” to just close the socket?

As long as a connection is alive, it is considered “not done” to just close a socket. Doing so will lead to a TCP RST packet, which is seen as a kind of error condition.

Which would make the code easier anyways instead of adding a timeout like you suggested in your referenced post.

It is what it is :-), you will need this time-out for a gracious shut-down.

Have you an idea why I could be loosing 64 Bytes? Do you have in mind some data structure of this size that gets re-allocated on every new connection?

As far as I know, there are no memory leaks in the FreeRTOS kernel or in any of the FreeRTOS libraries.
Sometimes, when I must find a memory leak, I put some short logging in pvPortMalloc() and vPortFree(), by adding a parameter that shows the origin in the source.

64 bytes is probably the minimum allocation size, so you’re allocating anything between 1 and 64 bytes.

Another thing that puzzles me is why the shutdown sometimes leaves the socket open and sometimes closes it. Is this related to the fact that the shutdown handshake needs time and so sometimes it manages to finish it and subsequently closes the socket as well, and sometimes it does not?

The function FreeRTOS_shutdown() only sets a flag. The next message to the peer will contain the FIN flag, unless there is pending data traffic. So yes, every shutdown needs time to complete, up to a few seconds ( on the Internet ).