I am using Ardunio/ESP32 and I am very new to FreeRTOS. I am trying to convert an int value to std::string
, since I don't have to_string
, so I used sprintf
to do so.
I noticed that when I call sprintf in the task function as follows ESP32 crashes and I can't find the reason.
bool PushMessageToSerial(const std::string &source, const std::string &message, log_t log)
void SensorActions(void *parameters)
{
PushMessageToSerial("Setup", "Sensors Monitor task is initialized", LOG_INFO);
while (1)
{
while (xQueueReceive(queue_sensors, (void *)&action, 10) == pdTRUE)
{
switch (action)
{
case actions_t::ACTION_SENSOR_VERSION:
{
uint8_t version[4] = {0};
sensor.GetVersions(version);
uint8_t length = sprintf(NULL, 0, "%d.%d.%d.%d", version[0], version[1], version[2], version[3]);
assert(length >= 0);
char *buf = new char[length + 1];
sprintf(buf, length + 1, "%d.%d.%d.%d", version[0], version[1], version[2], version[3]);
std::string text(buf);
free(buf);
PushMessageToSerial("Sensors", text, LOG_INFO);
}
break;
default:
break;
}
}
vTaskSuspend(NULL); // Auto suspend the task to not blocking the other lower priority tasks
}
}
can you please give me a hint to understand what is wrong?