esp32 crashes when cJSON_Delete is called

hp26_esp32
Posts: 5
Joined: Wed Jun 17, 2020 6:28 am

esp32 crashes when cJSON_Delete is called

Postby hp26_esp32 » Wed Jun 17, 2020 6:44 am

I Hope every one is doing fine. I am using esp32 for a project and it keeps on crashing as soon as cJSON_Delete() is called

my code

Code: Select all

printf("%s\r\n,buffer);
cJSON *root = cJSON_Parse(buffer);
char *data1 = cJSON_GetObjectItem(root, "data1")->valuestring;
char *data2 = cJSON_GetObjectItem(root, "data2")->valuestring;
printf("data1: %s | data2:%s\r\n",data1,data2);
cJSON_Delete(root);
crash happens every time after the printf to print data1 and data2

this is printed when esp32 crashes

CORRUPT HEAP: multi_heap.c:477 detected at 0x3ffcd910
abort() was called at PC 0x40091cd9 on core 0

ELF file SHA256: 36986c6c1341bb59

Backtrace: 0x4008f839:0x3ffcb3a0 0x4008fbbd:0x3ffcb3c0 0x40091cd9:0x3ffcb3e0 0x400823b6:0x3ffcb400 0x400950d5:0x3ffcb420 0x400fe4ce:0x3ffcb440 0x400fe4bd:0x3ffcb460 0x400d7184:0x3ffcb480 0x4009216d:0x3ffcb4b0

Rebooting...


Please point me in the correct direction to resolve this issue.

Thanks in advance.
Last edited by hp26_esp32 on Wed Jun 17, 2020 11:51 pm, edited 1 time in total.

boarchuz
Posts: 606
Joined: Tue Aug 21, 2018 5:28 am

Re: esp32 crashes when cJSON_Delete is called

Postby boarchuz » Wed Jun 17, 2020 7:40 am

You need to check return values for cJSON_Parse, cJSON_GetObjectItem, and any related functions. If they fail for any reason (no mem, invalid json, element not found, etc) they will return NULL.

hp26_esp32
Posts: 5
Joined: Wed Jun 17, 2020 6:28 am

Re: esp32 crashes when cJSON_Delete is called

Postby hp26_esp32 » Wed Jun 17, 2020 11:28 pm

boarchuz wrote:
Wed Jun 17, 2020 7:40 am
You need to check return values for cJSON_Parse, cJSON_GetObjectItem, and any related functions. If they fail for any reason (no mem, invalid json, element not found, etc) they will return NULL.
I checked
cJSON_Parse and cJSON_GetObjectItem none of them are null

char *buffer has this data
{"data1":"abc","data2":"xyz"}

cJSON_Parse is not null
char *data1 and char *data2 are not null either.

printf("data1: %s | data2:%s\r\n",data1,data2);
the values are printed properly.

The free heap memory is printed as well
printf("FREE MEMORY: %d\r\n",xPortGetFreeHeapSize());
FREE MEMORY: 195576

I also print the pointer of root
printf("Pointer to root: %p\r\n", root);

Pointer to root: 0x3ffcf8b0

after printing the pointer i call cJSON_Delete which crashes everything and prints

CORRUPT HEAP: multi_heap.c:477 detected at 0x3ffcda7c
abort() was called at PC 0x40091cd9 on core 0

ELF file SHA256: 36986c6c1341bb59

Backtrace: 0x4008f839:0x3ffcb3a0 0x4008fbbd:0x3ffcb3c0 0x40091cd9:0x3ffcb3e0 0x400823b6:0x3ffcb400 0x400950d5:0x3ffcb420 0x400fe4ce:0x3ffcb440 0x400fe4bd:0x3ffcb460 0x400d7184:0x3ffcb480 0x4009216d:0x3ffcb4b0

Rebooting...

boarchuz
Posts: 606
Joined: Tue Aug 21, 2018 5:28 am

Re: esp32 crashes when cJSON_Delete is called

Postby boarchuz » Thu Jun 18, 2020 3:03 am

If you comment out cJSON_Delete does it still crash?

hp26_esp32
Posts: 5
Joined: Wed Jun 17, 2020 6:28 am

Re: esp32 crashes when cJSON_Delete is called

Postby hp26_esp32 » Sat Jun 20, 2020 5:04 am

I used back trace and realized that the crash was caused by free() which is being called by another task. i removed it and everything works fine. boarchuz, Thank you for the suggestions.

Who is online

Users browsing this forum: Lobelois, ltsm0265 and 99 guests