diff --git a/FAQ.md b/FAQ.md index ee3fd14..ebb7304 100644 --- a/FAQ.md +++ b/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. - ##### Cause 1: reuse of `JsonBuffer` First, this can happen if you reuse the same `JsonBuffer`, for example: ```c++ StaticJsonBuffer<200> jsonBuffer; + for (int i=0; i<10; i++) { char json[256]; readJsonFromSomewhere(json, sizeof(json)); + JsonObject& root = jsonBuffer.parse(json); if (root.success()) { 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`? | `StaticJsonBuffer` | `DynamicJsonBuffer`