mirror of
https://github.com/bblanchon/ArduinoJson.git
synced 2025-08-07 06:34:43 +02:00
Why does my device crash or reboot?
45
FAQ.md
45
FAQ.md
@@ -7,7 +7,7 @@ See [Compatibility issues](Compatibility issues) first.
|
||||
No.
|
||||
|
||||
This is a fundamental design principle in this library.
|
||||
The JSON input must be in memory and must be mutable (ie not readonly) to allow zero-copy and zero-allocation, which is *the* strength of this library.
|
||||
The JSON input must be in memory and must be mutable (i.e., not read-only) to allow zero-copy and zero-allocation, which is *the* strength of this library.
|
||||
|
||||
Let's see an example to understand why this is important:
|
||||
|
||||
@@ -196,6 +196,45 @@ for (int i=0; i<10; i++) {
|
||||
|
||||
See issue [#153](https://github.com/bblanchon/ArduinoJson/issues/153) and [#160](https://github.com/bblanchon/ArduinoJson/issues/160).
|
||||
|
||||
### Why does my device crash or reboot?
|
||||
|
||||
99.999% of the time, this is caused by a "stack overflow", i.e. you have too many variables in the "stack".
|
||||
The solution is to move variables to the "heap".
|
||||
|
||||
First, replace the `StaticJsonBuffer` by a `DynamicJsonBuffer`.
|
||||
Then, use dynamic allocation for the JSON input.
|
||||
|
||||
For instance, if you have a program like this:
|
||||
|
||||
```c++
|
||||
char content[MAX_CONTENT_SIZE];
|
||||
StaticJsonBuffer<JSON_BUFFER_SIZE> jsonBuffer;
|
||||
|
||||
receive(content);
|
||||
JsonObject& root = jsonBuffer.parseObject(content);
|
||||
|
||||
Serial.println(root["name"].asString());
|
||||
```
|
||||
|
||||
you should transform it like that:
|
||||
|
||||
```c++
|
||||
char* content = malloc(MAX_CONTENT_SIZE);
|
||||
DynamicJsonBuffer jsonBuffer(JSON_BUFFER_SIZE);
|
||||
|
||||
receive(content);
|
||||
JsonObject& root = jsonBuffer.parseObject(content);
|
||||
|
||||
Serial.println(root["name"].asString());
|
||||
|
||||
free(content);
|
||||
```
|
||||
|
||||
Of course, this is only possible if your target has enough memory.
|
||||
Sometimes, it's just impossible because the MCU is too small.
|
||||
|
||||
See issues [#233](https://github.com/bblanchon/ArduinoJson/issues/233), [#234](https://github.com/bblanchon/ArduinoJson/issues/234) and [#262](https://github.com/bblanchon/ArduinoJson/issues/262)
|
||||
|
||||
### Parsing succeeds but I can't read the values!
|
||||
|
||||
99.999% of the time, this is caused by a confusion between arrays and objects.
|
||||
@@ -319,10 +358,10 @@ void myFunction(JsonVariant variant)
|
||||
{
|
||||
JsonObject& object = variant;
|
||||
// ...
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// ...
|
||||
// ...
|
||||
}
|
||||
}
|
||||
```
|
||||
|
Reference in New Issue
Block a user