This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Need some information on how memory on heap is managed

Other Parts Discussed in Thread: SYSBIOS
I have the following scenario

1. On CC3200LP server waiting for incoming connection.

2. Accept client connection

3. Spawn a new thread to handle connection in this way

	void InitTCPServer(int socket_id){

		Task_Params taskParams;

    		Task_Params_init(&taskParams);
		taskParams.stackSize = TASKSTACKSIZE;
		taskParams.stack = new char[TASKSTACKSIZE];
		taskParams.priority = 1;
		taskParams.arg0 = (xdc_UArg)new CThreadTCPClientHandle(socket_id);

    		Task_construct(new Task_Struct, (Task_FuncPtr)CThreadTCPClientHandle::Create, &taskParams, NULL);

	}
	
4. When client close connection thread terminate

   How handle free the heap memory created with new char[TASKSTACKSIZE], new Task_Struct and  

   (xdc_UArg)new CThreadTCPClientHandle(socket_id)?

   Are they automatically freed when Thread Terminate?	

Thanks Regards		
  • Hi Fortunato,

    You can free the dynamically allocated memory by posting a semaphore just before the Task function exits, and have another lower-priority Task pend on that semaphore. You'd have to keep pointers to these allocations so that the 'cleanup' Task pending on the semaphore can access them.

    If you do not mind letting the Task module allocate the task stack, another approach may be to turn on the config parameter 'Task.deleteTerminatedTasks' (see the SYSBIOS documentation for more details), use Task_create() instead of Task_construct() and set taskParams.stack to NULL. Then the Task structure and its stack would be automatically deleted. The Task function can take care of freeing the memory for the other memory allocations before exiting.

    Best regards,
    Vincent
  • About the stack memory you suggested a very nice idea.

    More complicated the way to post semaphore and pend on it to delete the memory.

    Maybe i will use a memory pool and flag it is as free so to reuse on next thread creation.

    Thanks regards

  • Hi Vincent
    What about using task hooks.
    Is there any example on how dynamically set a hook?
    Regards
  • Hi Fortunato,

    Task hook functions can only be configured statically. Using the Task exit hook to deallocate the Task stack wouldn't be my choice, as the exit hook function is called by Task_exit() while the Task stack is still in use.

    Best regards,
    Vincent