Hi Hervé
fnGetTelnetSession() can return 0 if called before starting TELNET (using fnStartTelnet()). Or it can also fail if fnStartTelnet() failed due to lack of memory.
The simulator will cause an exception when this happen, which makes it also very obvious that something is not right.
Now there are very probably different opinions on this, but I prefer to let the simulator get an exception so that the root problem (maybe simply that the user didn't realise that fnStartTelnet() must forst be called) rather than protect every point in code so that it can 'silently' fail and then effectively pass the basic problem on to later routines. At the end of the day the problem needs to be solved and I prefer such 'obvious' things to become immediately obvious. In addition it also saves a bit of code because there are less checks which are not necessary once everything is working (configured) correctly anyway.
uMalloc() doesn't allow freeing memory. Its use is as an alternative to declaring static variables but results in about the same situation [there are however advantages since by not activating certain services (eg. using their start calls) they can all be present in code but only the ones being used consume RAM - in some situations it may not be possible to run all services at the same time due to RAM constraints, but they can all be present and just the required one actually started]. malloc() from a library can also be used in parallel but the uTasker demo project and its components don't call it.
fnSendBufTCP() allocates the working TCP buffer space the first time that it is called on the particular socket. After that it already has this space allocated and so doesn't call uMalloc() again.
However this leads to the problem that you are seeing. This is because of the use of fnReleaseTCP_Socket(GIP_TCP_socket); when the connection is closed. This is in fact deleting the socket and all of its contents, as well as the pointer to the allocated TCP buffer. The next socket used will thus try to allocate the working memory again and it will eventually fail due to no more memory.
The solution is not to release the socket - there is no real reason to do this since you know that you will need it again. Generally set GIP_TCP_socket to -1 on initialisation and then only get the socket once with
if (GIP_TCP_socket < 0) fnGetTCP_Socket();
This should solve any problems that you are having.
If you need to re-initialise something else it can still be performed at the same location. See, for example, fnResetTelnetSession() in telnet.c, which is resetting some session information on each close.
On the other hand there is a problem in TELNET with its use of fnStopTelnet(). This also releases the socket and so if it is later started again it may fail due to lack of buffer memory. Normally TELNET is started and never stopped, so this has probably never been experienced (this code originates before the use of buffered TCP was added - there is no problem together with sockets that don't use a buffered TCP).
I will need to look into this a bit more before deciding what is best to do about it...
Regards
Mark