I am using Free RTOS on ESP32 in my program. However at some point of time when I execute xTaskCreate()
it returns -1 , the error being could_not_allocate_required_memory. In my program, each time I pring getFreeHeap()
it is reduced by 2,000 approximately and is never cleared or reset. When it is not sufficient or below 2,000 xTaskCreate returns -1.
After reading a lot of forums I could not find a way to clear the memory to create a task. Can anyone guide me with this?
code snippet
void createTsk(){
SerialDebug.print("available Heap size: ");
SerialDebug.println(ESP.getFreeHeap());
BaseType_t xBy = xTaskCreate(taskPriorityOne, "TaskOne", 20240, NULL, 1, &thOne);
SerialDebug.println("Return Val : ");
SerialDebug.println(xBy);
}
void taskPriorityOne(void *paramter)
{
SerialDebug.println("Prio 1 task Initiated");
threadTask(priorityOneQue, "task1", thOne);
}
void threadTask(QueueHandle_t priorityQue, String taskName, TaskHandle_t th)
{
xQueueReceive(priorityQue, &taskdataholder, portMAX_DELAY);
ongoingProcess = taskdataholder;
furtherExecutionHttp(taskdataholder, fnm, taskName, th);
}
void furtherExecutionHttp(DataHolder *taskdataholder, String fnm, String
taskName, TaskHandle_t th)
{
String response = httpRequest(taskdataholder->protocol, taskdataholder-
>host, taskdataholder->port, taskdataholder->url, taskdataholder->body,
fnm);
delay(10);
free(taskdataholder);
SerialDebug.println("Releasing memory..\n");
vTaskDelete(th);
}
delete
operator.thOne
is a global variable, this only works if only one task is running at a time. If you start a new task while the old has not exited, the next call to xTaskDelete will delete the task that was most recently started, not the current one. TryxTaskDelete(null)
to delete the current task.