esp32 resets after memory allocation failed by asprintf

burkulesomesh43
Posts: 132
Joined: Tue Aug 14, 2018 6:21 am
Location: India

esp32 resets after memory allocation failed by asprintf

Postby burkulesomesh43 » Fri Oct 19, 2018 8:12 pm

Hi,
I want to use asprintf for getting large string and dynamic allocation, but when i am using in my task, memory allocation gets failed.
and esp32 resets. what is wrong in my code?

Code: Select all

void producer_task(void *pvParameter){

	char *mydata=NULL;

    while(1){

    	vTaskDelay(10);
    	int ret=asprintf(&mydata,"abcdefghijklmnopqrstuvwxyzxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
		if(ret==-1)
		 {
		   ESP_LOGI(tag, "asprintf:memory allocation failed");

		 }

		 ESP_LOGI(tag, "single_tag:%s", mydata);
		 if(xQueueSend( xQueue, ( void * )&mydata, ( portTickType )0 )==errQUEUE_FULL)
		  {
		  ESP_LOGI("Queue","Queue is full..");
		  }
		 mydata=NULL;
		 free(mydata);

    }

}
--
Somesh Burkule

WiFive
Posts: 3529
Joined: Tue Dec 01, 2015 7:35 am

Re: esp32 resets after memory allocation failed by asprintf

Postby WiFive » Fri Oct 19, 2018 8:58 pm

Check free memory with heap_caps_get_free_size. Don't free the memory until it is received from the queue. Keep all your related questions in one topic. Learn the basics of memory management.

burkulesomesh43
Posts: 132
Joined: Tue Aug 14, 2018 6:21 am
Location: India

Re: esp32 resets after memory allocation failed by asprintf

Postby burkulesomesh43 » Sat Oct 20, 2018 4:21 am

WiFive wrote:Check free memory with heap_caps_get_free_size. Don't free the memory until it is received from the queue. Keep all your related questions in one topic. Learn the basics of memory management.
I used that api. as the task is created the free size is (MALLOC_CAP_32BIT 132752), after some time as use free() the free size become
(MALLOC_CAP_32BIT 60432) and shows error memory allocation failed and esp32 resets.

at the starting free size is..
heap_init: At 3FFAFF10 len 000000F0 (0 KiB): DRAM[0m
[0;32mI (476) heap_init: At 3FFD2B70 len 0000D490 (53 KiB): DRAM[0m
[0;32mI (483) heap_init: At 3FFE0440 len 00003BC0 (14 KiB): D/IRAM[0m
[0;32mI (489) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM[0m
[0;32mI (495) heap_init: At 40091514 len 0000EAEC (58 KiB): IRAM[0m
--
Somesh Burkule

WiFive
Posts: 3529
Joined: Tue Dec 01, 2015 7:35 am

Re: esp32 resets after memory allocation failed by asprintf

Postby WiFive » Sat Oct 20, 2018 7:25 am

You are probably leaking memory. You should use MALLOC_CAP_DEFAULT.

burkulesomesh43
Posts: 132
Joined: Tue Aug 14, 2018 6:21 am
Location: India

Re: esp32 resets after memory allocation failed by asprintf

Postby burkulesomesh43 » Sat Oct 20, 2018 9:15 am

WiFive wrote:You are probably leaking memory. You should use MALLOC_CAP_DEFAULT.
same case happenned..

;32mI (194675) ss: MALLOC_CAP_DEFAULT 152[0m
[0;32mI (194675) ble: asprintf:memory allocation failed[0m
Guru Meditation Error: Core 1 panic'ed (LoadProhibited)
. Exception was unhandled.
Core 1 register dump:
PC : 0x400014fd PS : 0x00060630 A0 : 0x800de69c A1 : 0x3ffecae0
A2 : 0x00000000 A3 : 0xfffffffc A4 : 0x000000ff A5 : 0x0000ff00
A6 : 0x00ff0000 A7 : 0xff000000 A8 : 0x00000000 A9 : 0x3ffeca90
A10 : 0x00000000 A11 : 0x3f403f94 A12 : 0x3ffecd24 A13 : 0x3ffd2cc4
A14 : 0x00000000 A15 : 0x00000001 SAR : 0x00000004 EXCCAUSE: 0x0000001c
EXCVADDR: 0x00000000 LBEG : 0x400014fd LEND : 0x4000150d LCOUNT : 0xffffffff

Backtrace: 0x400014fd:0x3ffecae0 0x400de699:0x3ffecaf0 0x400dfe89:0x3ffece00 0x40082cf1:0x3ffece30 0x400d4c04:0x3ffece80

Rebooting...
--
Somesh Burkule

ESP_Angus
Posts: 2344
Joined: Sun May 08, 2016 4:11 am

Re: esp32 resets after memory allocation failed by asprintf

Postby ESP_Angus » Mon Oct 22, 2018 6:44 am

Code: Select all

mydata=NULL;
free(mydata);
You need to swap the order of these two lines, or mydata is never freed - free(NULL) does nothing.

ESP_Angus
Posts: 2344
Joined: Sun May 08, 2016 4:11 am

Re: esp32 resets after memory allocation failed by asprintf

Postby ESP_Angus » Mon Oct 22, 2018 6:47 am

The reason for the crash is that after you log

Code: Select all

   ESP_LOGI(tag, "asprintf:memory allocation failed");
... mydata is NULL. Then when you try to log this as a string, it crashes.

You need to change your program logic so it won't try to use "mydata" if asprintf fails.

(although if you fix the memory leak as shown in the reply above, this should happen less often!)

burkulesomesh43
Posts: 132
Joined: Tue Aug 14, 2018 6:21 am
Location: India

Re: esp32 resets after memory allocation failed by asprintf

Postby burkulesomesh43 » Mon Oct 22, 2018 5:05 pm

ESP_Angus wrote:

Code: Select all

mydata=NULL;
free(mydata);
You need to swap the order of these two lines, or mydata is never freed - free(NULL) does nothing.
issue solved. thanks.
need to free first.
actually now am using free in queue recieve so that sent data in queue will freed after recieved from queue.
when it freed directly after queue send data loss happens.
--
Somesh Burkule

Who is online

Users browsing this forum: No registered users and 120 guests