Calling freeaddrinfo causes CORRUPT_HEAP and a subsequent reboot.

Zingemneire
Posts: 68
Joined: Tue Apr 17, 2018 7:35 am

Calling freeaddrinfo causes CORRUPT_HEAP and a subsequent reboot.

Postby Zingemneire » Wed Sep 12, 2018 7:22 am

Hi,

In doing some testing to try and resolve this problem "viewtopic.php?f=2&t=7152" I experimented by adding a call to freeaddrinfo once the connection to the socket is established.

Earlier I commented out that call because when I called freeaddrinfo in case a connection could not be established ( after a +/- 18 second timeout ) I got an instant CORRUPT_HEAP error message and a subsequent reboot.

Reactivating the call once the socket is connected causes slightly different behaviour:
  • The software does not crash instantly but carries on for a few seconds.
  • Then I get the familiar CORRUPT_HEAP message and the reboot.
It is perfectly reproducible: comment out the call, build and flash => normal behaviour, uncomment it, build and flash => CORRUPT_HEAP message and reboot.

I suppose both problems might be related but I have not got a clue as to what is going wrong. Does anyone have any ideas or suggestions?

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

Re: Calling freeaddrinfo causes CORRUPT_HEAP and a subsequent reboot.

Postby ESP_Angus » Wed Sep 12, 2018 7:34 am

Can you post the code which calls freeaddrinfo()? Is it freeing a pointer which was returned by a call to getaddrinfo()?

freeaddrinfo() passes the structures inside the addrinfo structure to free(). free() will crash with a CORRUPT HEAP message if you try to free something which wasn't allocated from malloc() - as this indicates either a corrupt heap or a very severe logic error.

If the addrinfo structure is returned correctly from getaddrinfo(), another possibility is that memory is being corrupted by some other code.

Zingemneire
Posts: 68
Joined: Tue Apr 17, 2018 7:35 am

Re: Calling freeaddrinfo causes CORRUPT_HEAP and a subsequent reboot.

Postby Zingemneire » Wed Sep 12, 2018 10:50 am

Thanks ESP_Angus but in the mean time the mystery is solved and cured.

There were 2 calls to freeaddrinfo using the same pointer, the first one worked but the second one failed.
I have made the call to freeaddrinfo conditional on there being a valid pointer ( NOT NULL ) and when it has been called set the pointer to NULL => problem solved, even if another part sill tries to make the same call.
Instead of directly calling freeaddrinfo I call this function:

Code: Select all

void Http_FreeAddressInfo ( void )
{
   if ( SocketResult )
   {
      freeaddrinfo ( SocketResult );
      SocketResult = NULL;
   }
}

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

Re: Calling freeaddrinfo causes CORRUPT_HEAP and a subsequent reboot.

Postby ESP_Angus » Thu Sep 13, 2018 12:09 am

Zingemneire wrote: There were 2 calls to freeaddrinfo using the same pointer, the first one worked but the second one failed.
I have made the call to freeaddrinfo conditional on there being a valid pointer ( NOT NULL ) and when it has been called set the pointer to NULL => problem solved, even if another part sill tries to make the same call.
Instead of directly calling freeaddrinfo I call this function:
Great. Yes, a "double-free" is always going to be a fatal error.

BTW, it is safe to call both free(NULL) and freeaddrinfo(NULL), so the null check itself is optional.

Zingemneire
Posts: 68
Joined: Tue Apr 17, 2018 7:35 am

Re: Calling freeaddrinfo causes CORRUPT_HEAP and a subsequent reboot.

Postby Zingemneire » Thu Sep 13, 2018 8:57 am

Thanks, that is good to know, it will simplify the code a bit.

Who is online

Users browsing this forum: davidv, Majestic-12 [Bot] and 109 guests