From f6d34201822f524079cbb7a882351b1f08b411be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Blanchon?= Date: Fri, 16 Oct 2015 21:52:56 +0200 Subject: [PATCH] Updated FAQ (markdown) --- FAQ.md | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/FAQ.md b/FAQ.md index d2ecade..6b6b9e0 100644 --- a/FAQ.md +++ b/FAQ.md @@ -42,4 +42,20 @@ See also: [issue #101](https://github.com/bblanchon/ArduinoJson/issues/101) and Some platforms need some time to initialize the serial port. In that case, you need to add a `delay()` in the setup. -See issue [#112](https://github.com/bblanchon/ArduinoJson/issues/112). \ No newline at end of file +See issue [#112](https://github.com/bblanchon/ArduinoJson/issues/112). + +### I found a memory leak in the library! + +This is very unlikely. You're probably using the library incorrectly. + +The typical problem comes from reusing a `JsonBuffer` several time. +Each time you call `parseArray()`, `parseObject()`, `createArray()` and `createObject()`, you consume memory in the `JsonBuffer`. +To avoid running out of memory, you should discard unused data as soon as possible. + +The recommended practice is to do the JSON handling in a dedicated function, with a local `JsonBuffer` that will be automatically reclaimed when the function exits. +This means that you cannot return a `JsonArray` or a `JsonObject` from that function, because they would contains dangling pointers to what used to be the `JsonBuffer`. +Instead, you should convert the content of the `JsonArray` to a custom array or `vector`; or the content of the `JsonObject` to your own data structure. + +This seems like a constrain, but remember that you're programming for an embedded platform with very limited resources; and that requires special techniques. + +A positive side effect of following this recommandation is that the code is safe and memory efficient. It also encourage the separation of responsibilities: the function is only in charge of the JSON serialization an no specific JSON data leaks elsewhere is the program. \ No newline at end of file