How to free allocated memory correctly? - Fatal exception / E:M 1584

Stational
Posts: 42
Joined: Sat Jul 02, 2016 10:54 pm

How to free allocated memory correctly? - Fatal exception / E:M 1584

Postby Stational » Sat Sep 10, 2016 8:40 pm

Hi,

i allocate memory for an array of structs, this is my code:

Code: Select all

      size_t taskAmount;
      object *tasks = parseJSON(subbuff,sizeof(subbuff),&taskAmount);
      os_free(tasks);


without that code my application works fine, but if i use it, after a certain time my application crashes and i get this text written on the serial terminal:

Fatal exception 9(LoadStoreAlignmentCause):
epc1=0x4024035f, epc2=0x00000000, epc3=0x00000000, excvaddr=0x4010045b, depc=0x00000000


I think that this error might have to do with too less available memory? This is how i allocate the memory in the parseJSON function:

Code: Select all

object *retObj = (object*)os_zalloc(sizeof(object) * 10);


How can i fix this error?

Edit: If i remove this line of code:

Code: Select all

 code os_free(tasks)


my code works for ~30 minutes.

But then the esp8266 won't connect to the network again, for example, this is my user_dns_found function (which is called by user_dns_check callback):

Code: Select all

LOCAL void ICACHE_FLASH_ATTR
user_dns_found(const char *name, ip_addr_t *ipaddr, void *arg)
{
    struct espconn *pespconn = (struct espconn *)arg;

    if (ipaddr == NULL)
    {
        os_printf("user_dns_found NULL \r\n");
        return;
    }

   //dns got ip
    os_printf("user_dns_found %d.%d.%d.%d \r\n",
            *((uint8 *)&ipaddr->addr), *((uint8 *)&ipaddr->addr + 1),
            *((uint8 *)&ipaddr->addr + 2), *((uint8 *)&ipaddr->addr + 3));

    if (tcp_server_ip.addr == 0 && ipaddr->addr != 0)
    {
      // dns succeed, create tcp connection
        tcp_server_ip.addr = ipaddr->addr;
        os_memcpy(pespconn->proto.tcp->remote_ip, &ipaddr->addr, 4); // remote ip of tcp server which get by dns

        pespconn->proto.tcp->remote_port = 80; // remote port of tcp server

        pespconn->proto.tcp->local_port = espconn_port(); //local port of ESP8266

        espconn_regist_connectcb(pespconn, user_tcp_connect_cb); // register connect callback
        espconn_regist_reconcb(pespconn, user_tcp_recon_cb); // register reconnect callback as error handler

        espconn_connect(pespconn); // tcp connect
    }
}


Normally the esp8266 would connect and call a function but instead it just prints

E:M 1584
user_dns_found 1.2.3.4


What does E:M 1584 mean and why does the esp8266 stop to connect to the server?

pratik

Re: How to free allocated memory correctly? - Fatal exception / E:M 1584

Postby pratik » Sun Sep 11, 2016 6:16 pm

Hello,

If you do not use os_free, there will be a memory leak resulting in decrease in free memory every time the new objects are created (but not freed). Soon there will be too little memory for the internal communications stack to operate properly, thus the connection issue.
If you do not REALLY need dynamic memory allocation, please stick to using global variables (heap variables). Create a "maximum" object array that you expect you would be needing and use this. If your object array size requirement varies too much, say by larger than 4kB, only then you should use dynamic allocation. You can print free memory using system_get_free_heap_size() API to find out the amount of free memory.

As for the exception, you are getting the error because of access to word-unaligned address in the system. Please do not use direct pointer casting and follow the programming guides closely. You may pinpoint the cause by locating the exact line that is causing the error in the .S listing generated when the project is compiled.

Who is online

Users browsing this forum: No registered users and 56 guests