Open Szczempus opened 1 month ago
Hi @Szczempus,
This looks like a stack corruption, but I cannot see the reason on the sample code.
Can you provide an MCVE that doesn't rely on MQTT, strftime
, and FreeRTOS?
Best regards, Benoit
While working on the proper code example, I found that this problem is likely related to FreeRTOS memory allocation. When I ran the sample code for the first time in the default loop (with all functions executed sequentially), everything ran smoothly for hours without any issues. The second time, I ran the code within a single FreeRTOS task, which also worked without errors. However, when I separated one part of my code to read values from sensors and another to read values from the structure and send them via MQTT in different tasks, the errors reappeared. I use mutexes to access variables and structures, so there shouldn't be any race conditions. I suspect that using heap allocation through FreeRTOS functions (pvPortMalloc()/vPortFree()) might resolve the issue? I will give it a try.
I'm not familiar with FreeRTOS, but using pvPortMalloc()
and vPortFree()
sounds like a good idea.
ArduinoJson allows replacing the allocator by implementing the ArduinoJson::Allocator
interface and passing an instance to JsonDocument
's constructor.
See: https://arduinojson.org/v7/api/jsondocument/#allocator
However, ArduinoJson::Allocator::reallocate()
requires a realloc()
-like behavior, which unfortunately is not built-in FreeRTOS (there is no pvPortRealloc()
), so you'll have to implement the functionality from pvPortMalloc()
and vPortFree()
.
Hello guys. Recently, I've been dealing with a persistent error that might indicate a library issue. I have a freeRTOS-based system running on an ESP32-WROVER-E. One of the tasks is responsible for sending serialized data via GSM to an MQTT broker. This is where the problem starts. As I've noticed, exiting the function where a JsonDocument object is created triggers a LoadProhibited error and restarts the ESP. As the backtrace suggests, the library tries to deallocate an empty memory space, which causes the error (Backtrace below). What I have tried is declaring StaticJsonDocument instead of JsonDocument, manually clearing the allocated memory using doc.clear(), and increasing the allocated task stack as well as the buffer size for the message. Unfortunately, none of these actions have worked. What do you think could be the cause of this problem and how can it be resolved? Thanks in advance.
Troubleshooter report
Here is the report generated by the ArduinoJson Troubleshooter:
PROGMEM
JsonDocument
char[]
String
doesn't solve the issueEnvironment
Here is the environment that I'm using':
Reproduction
Here is a small snippet that demonstrate the problem.
Program output
If relevant, include the program output.
Actual output: