TCP server, socket and memory

Hello,

Right now I’m developing a simple TCP server inside a Connect ME. I’ve downloaded the TCP Server example from digi.com

But after I’ve looked at the code, there are a few things I don’t understand. After a client has connected to the server, a new thread is created to listen to that connection. The code is the following:

        stackP=malloc(4096);
    dyn_thread = malloc(sizeof(TX_THREAD));
    dyn_name = malloc(20);
    
    sprintf(dyn_name, "Com_Thread #%d", thread_number++);
    
    retVal = tx_thread_create (dyn_thread,                      /* control block for root thread*/
	    dyn_name,                      /* thread name*/
	    datapipe,                       /* entry function*/
	    clientSocketDescriptor,             /* parameter*/
	    stackP,                /* start of stack*/
	    2048,                /* size of stack*/
	    16,                  /* priority*/
	    16,                  /* preemption threshold */
	    1,                                  /* time slice threshold*/
	    TX_AUTO_START);                     /* start immediately*/

As you can see, the stack of the thread is taken from the heap. But it’s never freed.

So, maybe I’m missing something but after some time this Digi will run out of memory. Because the thread doesn’t free that memory on destruction.

Thank you!

Absolutely! Check out tx_thread_create and tx_thread_delete in the API. It’s the applications responsibility to call free() when the thread has been removed to free up the memory.

Hi alblurrow

Where can I find that example because I am writing a very similar program and a guideline would be nice to have

Hi,
check the programmers guide. You may find it in Start->Programs->Digi->documentation

Jirka