CORRUPT HEAP

quentho
Posts: 4
Joined: Mon Jun 01, 2020 3:08 pm

CORRUPT HEAP

Postby quentho » Mon Jun 01, 2020 3:18 pm

Hello,

i would like to put my serial char result inside char* with concatenation function.
But i have a 2 problem problemes :
CORRUPT HEAP: Bad tail
CORRUPT HEAP: multi_heap.c:432
i think it's overload but i initialize my pointer with 15 octets and normaly it's enought.
  1. void Wisol::getID()
  2. {
  3.   printf("getID");
  4.   char * data=(char *) malloc(15);
  5.   char  receive;
  6.   *(data)='\0';
  7.   Serial2.begin(9600, SERIAL_8N1, RXPin, TXPin);
  8.   Serial2.println("AT$I=10");
  9. delay(1000);
  10.   while (Serial2.available()) {
  11.    //Serial.println(char(Serial2.read()));
  12.    delay(100);
  13.    receive= Serial2.read();
  14.    printf("data: %c \n",receive);
  15.    strcat( data, &receive );
  16.   }
  17.     free(data);
  18. }
X
Attachments
corruption error.PNG
corruption error.PNG (28.38 KiB) Viewed 5585 times

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

Re: CORRUPT HEAP

Postby ESP_Angus » Mon Jun 01, 2020 11:29 pm

You can't use "strcat()" with a single char, it takes a null-terminated string. If the byte after "receive" in the RAM is a zero, it will work as expected. If the byte is anything else, it will keep concatenating those bytes to the string until it sees a zero. This could be a lot more than 15 bytes!

quentho
Posts: 4
Joined: Mon Jun 01, 2020 3:08 pm

Re: CORRUPT HEAP

Postby quentho » Thu Jun 04, 2020 9:41 am

I don't understand because i have set the termined zero terminal with this line : *(data+1)='\0';. It's working but the probleme is when i free my pointer.
I show you the new code with the same result
Attachments
error with free comment.PNG
error with free comment.PNG (54.48 KiB) Viewed 5500 times

ESP_Sprite
Posts: 9730
Joined: Thu Nov 26, 2015 4:08 am

Re: CORRUPT HEAP

Postby ESP_Sprite » Thu Jun 04, 2020 7:08 pm

Now you're allocating only one char for data and data_int, and you're writing more than one char to it. (The zero terminators to start with, and the strcat also overwrites the end of data).

quentho
Posts: 4
Joined: Mon Jun 01, 2020 3:08 pm

Re: CORRUPT HEAP

Postby quentho » Fri Jun 05, 2020 9:00 am

I increased the size of my pointers with new char [15], I have the right concatenation but I still have a problem with my memory free call CORRUPT HEAP tail.
Attachments
error after free.PNG
error after free.PNG (34.81 KiB) Viewed 5416 times

ESP_Sprite
Posts: 9730
Joined: Thu Nov 26, 2015 4:08 am

Re: CORRUPT HEAP

Postby ESP_Sprite » Sat Jun 06, 2020 9:17 am

How do you get to 15? According to the log of your program, data contains '7OK\r\nOK\r\nOK\r\n00352643\r\n' (with \r\n being carriage return / newline, which won't show up as individual characters on your terminal) which is way longer than 15.

Who is online

Users browsing this forum: Majestic-12 [Bot] and 96 guests