ESP32 & cJSON - Fail to create large cJSON arrays

ferni9
Posts: 4
Joined: Fri Mar 27, 2020 11:29 am

ESP32 & cJSON - Fail to create large cJSON arrays

Postby ferni9 » Fri Apr 03, 2020 2:28 pm

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.

nvannote
Posts: 51
Joined: Thu Nov 14, 2019 10:42 pm

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

Postby nvannote » Sun Apr 05, 2020 8:04 am

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

ferni9
Posts: 4
Joined: Fri Mar 27, 2020 11:29 am

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

Postby ferni9 » 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 ;)

nvannote
Posts: 51
Joined: Thu Nov 14, 2019 10:42 pm

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

Postby nvannote » Thu Apr 09, 2020 6:06 am

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

Who is online

Users browsing this forum: robizzar72 and 124 guests