Enthernet Code
Enthernet Code
DIIDevHeads IoT Integration Server
Created by Marvee Amasi on 6/11/2024 in #šŸ“¦-middleware-and-os
What is wrong with my code ?
@Marvee Amasi your code is crashing mainly because of three mistakes you are making which are: using the new operator to allocate memory for buf while you are freeing it using free(). This is incorrect and can lead to memory leaks or crashes. Since you are using C++, it is recommended to use new and delete for memory management. secondly, In the sprintf call you are passing length + 1 as the buffer length argument. whereas the buffer length argument should be the size of the buffer, not the size of the string you are going to write into it. You can simply pass length as the buffer length. thirdly, In the sprintf call, you are using a format specifier of %d for each version number. However, since the version numbers are uint8_t you should use the format specifier %hhu instead. your code should look like this šŸ‘‡
c++
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);
char buf[16];
int length = sprintf(buf, "%hhu.%hhu.%hhu.%hhu", version[0], version[1], version[2], version[3]);
assert(length >= 0);
std::string text(buf, length);
PushMessageToSerial("Sensors", text, LOG_INFO);
}
break;
default:
break;
}
}
vTaskSuspend(NULL); // so as to auto suspend task to not block the other lower priority tasks
}
}
c++
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);
char buf[16];
int length = sprintf(buf, "%hhu.%hhu.%hhu.%hhu", version[0], version[1], version[2], version[3]);
assert(length >= 0);
std::string text(buf, length);
PushMessageToSerial("Sensors", text, LOG_INFO);
}
break;
default:
break;
}
}
vTaskSuspend(NULL); // so as to auto suspend task to not block the other lower priority tasks
}
}
7 replies