Hi,
I have an issue with my sketch which receive data using MySensors library, and parse it for my needs. After a 8-24 hours (depending on data receiving frequency) of proper working my arduino freezes. could you please help me with the following code? I think I could have memory leaks in one of the functions which actually performs the received data parsing. The sketch works fine if the function call is commented out, and works bad if the call is enabled. So this is the function:
void parseReading(char * receivedReadings, int sensorId) {
char *strtokIndx;
strtokIndx = (char*)malloc(25);
char tempDate[17] = {0};
//temp
strtokIndx = strtok(receivedReadings,"/");
dtostrf(static_cast<float>(atoi(strtokIndx))/10., 6, 1, readings[sensorId].temp);
//hum
strtokIndx = strtok(NULL, "/");
dtostrf(static_cast<float>(atoi(strtokIndx))/10., 5, 1, readings[sensorId].hum);
strcat(readings[sensorId].hum,"%");
//date and time
strtokIndx = strtok(NULL, "/");
strncpy(tempDate,strtokIndx,4);
strcat(tempDate, "-");
strncat(tempDate,strtokIndx+4,2);
strcat(tempDate, "-");
strncat(tempDate,strtokIndx+6,2);
strcat(tempDate, " ");
strncat(tempDate,strtokIndx+8,2);
strcat(tempDate, ":");
strcat(tempDate,strtokIndx+10);
strcpy(readings[sensorId].date,tempDate);
free(strtokIndx);
}
The call is:
parseReading(receivedMsg,sensor);
where:
receivedMsg is:
char receivedMsg[25] = {0};
declared in the same scope when the call is performed and
sensor
is an uint8_t
(yeah I know that uint8_t and int are different, noticed this when writing the post but I do not think it could cause my issue).
The received string is in the following format:
123/456/201903061130
and after the parse I expect data stored in the following struct:
struct reading {
char header[11] = {0};
char temp[7] = {0};
char hum[7] = {0};
char date[17] = {0};
};
reading readings[4];
which is a global variable. And the data looks like this (based on above example):
temperature is 12.3
humidity is 45.6
date is 2019-03-06 11:30
I can post a complete sketch, however it is quite long and do not think it has an error somewhere else. I am not very good at programming in C/C++ and think I messed up with memory allocation and freeing it at the end of the function.