What is wrong with my code ?

Hi everyone @Middleware & OS , please what is wrong with my code ?
void SensorActions(void *parameters)
{
 PushMessageToSerial("Setup", "Sensor's 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); // so as to auto suspend task to not block the other lower priority tasks
    }
}

I'm using Arduino/ESP32 with FreeRTOS and I'm aware std::string isn't available. That's why I used sprintf to convert an int value to a string. Now 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)
Was this page helpful?