From 9fcb69452e459d513691df436dffd60b7cfa3ef2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Blanchon?= Date: Sun, 8 Nov 2015 09:55:28 +0100 Subject: [PATCH] Updated FAQ (markdown) --- FAQ.md | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) 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`