Page 1 of 1

ESP32 & cJSON - Fail to create large cJSON arrays

Posted: Fri Apr 03, 2020 2:28 pm
by ferni9
Hello!
I am filling a cJSON Array with AddNumberToObject() function. At first it works fine, but at some point the add_item_to_object() function within AddNumberToObject() begins to return NULL, thus not writing the number and producing afterwards the error "LoadProhibited", when dereferencing the null pointer (char *) to this Array rendered as text.
The cJSON Array I am trying to create is quite large: 3200 times AddNumberToObject() function.

Is there maybe a maximum size for a cJSON array? Or am I simply running out of memory in ESP32?

Any help would be really appreciated.

Re: ESP32 & cJSON - Fail to create large cJSON arrays

Posted: Sun Apr 05, 2020 8:04 am
by nvannote
I took a quick look at the cJSON implementation and yes, at 3200 add(s), I am confident you are hitting the heap ceiling. It’s not a limitation of cJSON itself. There isn’t much that can go wrong in AddNumberToObject other than an allocation failure.

Never assume something succeeded, check results and fail early. :)

To whom it may concern (at Espressif) in regards to cJSON in IDF:

I noticed that cJSON doesn’t store its “generic” data items into a union; so the cJSON data structs always have some unused members depending on the datatype being stored. Granted, it probably would not have made the difference here; but for 3200 items it would save ~25K in struct overhead. Just saying. :)


Regards

Re: ESP32 & cJSON - Fail to create large cJSON arrays

Posted: Tue Apr 07, 2020 7:51 pm
by ferni9
You were completely right, I was running out of heap space, thank you!
Solution to this is quite straightforward though: create many smaller arrays and send/free them one by one, instead of a larger one ;)

Re: ESP32 & cJSON - Fail to create large cJSON arrays

Posted: Thu Apr 09, 2020 6:06 am
by nvannote
ferni9 wrote:
Tue Apr 07, 2020 7:51 pm
You were completely right, I was running out of heap space, thank you!
Solution to this is quite straightforward though: create many smaller arrays and send/free them one by one, instead of a larger one ;)
Exactly...

Cheers