mirror of
https://github.com/bblanchon/ArduinoJson.git
synced 2025-08-18 03:51:21 +02:00
Updated FAQ (markdown)
23
FAQ.md
23
FAQ.md
@@ -18,16 +18,17 @@ If you're in case 3., you may have a memory leak, it up to you to find it. You c
|
|||||||
|
|
||||||
This can be due to two causes.
|
This can be due to two causes.
|
||||||
|
|
||||||
|
|
||||||
##### Cause 1: reuse of `JsonBuffer`
|
##### Cause 1: reuse of `JsonBuffer`
|
||||||
|
|
||||||
First, this can happen if you reuse the same `JsonBuffer`, for example:
|
First, this can happen if you reuse the same `JsonBuffer`, for example:
|
||||||
|
|
||||||
```c++
|
```c++
|
||||||
StaticJsonBuffer<200> jsonBuffer;
|
StaticJsonBuffer<200> jsonBuffer;
|
||||||
|
|
||||||
for (int i=0; i<10; i++) {
|
for (int i=0; i<10; i++) {
|
||||||
char json[256];
|
char json[256];
|
||||||
readJsonFromSomewhere(json, sizeof(json));
|
readJsonFromSomewhere(json, sizeof(json));
|
||||||
|
|
||||||
JsonObject& root = jsonBuffer.parse(json);
|
JsonObject& root = jsonBuffer.parse(json);
|
||||||
if (root.success()) {
|
if (root.success()) {
|
||||||
Serial.println("parseObject() succeeded");
|
Serial.println("parseObject() succeeded");
|
||||||
@@ -37,6 +38,26 @@ for (int i=0; i<10; i++) {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
The solution is simply to NOT reuse the `JsonBuffer`, like this:
|
||||||
|
|
||||||
|
```c++
|
||||||
|
for (int i=0; i<10; i++) {
|
||||||
|
char json[256];
|
||||||
|
readJsonFromSomewhere(json, sizeof(json));
|
||||||
|
|
||||||
|
StaticJsonBuffer<200> jsonBuffer;
|
||||||
|
|
||||||
|
JsonObject& root = jsonBuffer.parse(json);
|
||||||
|
if (root.success()) {
|
||||||
|
Serial.println("parseObject() succeeded");
|
||||||
|
} else {
|
||||||
|
Serial.println("parseObject() failed!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Note that, contrary to a common belief, moving a `StaticJsonBuffer` inside of a loop has no negative impact on performance.
|
||||||
|
|
||||||
### What are the differences between `StaticJsonBuffer` and `DynamicJsonBuffer`?
|
### What are the differences between `StaticJsonBuffer` and `DynamicJsonBuffer`?
|
||||||
|
|
||||||
| `StaticJsonBuffer` | `DynamicJsonBuffer`
|
| `StaticJsonBuffer` | `DynamicJsonBuffer`
|
||||||
|
Reference in New Issue
Block a user