Page 1 of 1

CORRUPT HEAP

Posted: Mon Jun 01, 2020 3:18 pm
by quentho
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

Re: CORRUPT HEAP

Posted: Mon Jun 01, 2020 11:29 pm
by ESP_Angus
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!

Re: CORRUPT HEAP

Posted: Thu Jun 04, 2020 9:41 am
by quentho
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

Re: CORRUPT HEAP

Posted: Thu Jun 04, 2020 7:08 pm
by ESP_Sprite
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).

Re: CORRUPT HEAP

Posted: Fri Jun 05, 2020 9:00 am
by quentho
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.

Re: CORRUPT HEAP

Posted: Sat Jun 06, 2020 9:17 am
by ESP_Sprite
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.