malloc causes hardfault on RT1064

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

malloc causes hardfault on RT1064

1,519 Views
Daan
Contributor III

Hi,

Our project uses pretty much storage and RAM space, however, it compiles and runs perfectly. I notice that after the application runs for a while, maybe 10 minutes, maybe 1 hour, it just crashes, if I examine the stack trace, it always crashes inside the malloc function, I think that's strange, if malloc fails for some reason, it should return NULL, it should not cause a hardfault. I've setup a test program which will always fail after some time:

static void memtest(int16_t* state)
{
  volatile uint16_t siz = rand() & 0xfff; // Make sure the amount is not too much
  uint8_t* p = malloc(siz);
  static uint8_t n = 0;

  if (p == NULL)
  {
    VT100_printf(" Malloc fail!!!\r\n");
    *state = TASKSWAITSIGNAL;
  }
  else
  {
    free(p);
    if (n == 50)
    {
      VT100_printf(".");
      n = 0;
    }
    else n++;
  }
}

This function is called periodically (500x/sec). It may run for an hour, or for 1 minute. The configured heap size is big enough, see the attached settings screenshot.

Furthermore, the application uses FreeRTOS with LWIP and the ethernet/MQTT stack.

Please also find attached a screenshot of the hardfault, an unaligned address allocation seems to cause the problem...

 

BR,

Daan

0 Kudos
Reply
4 Replies

1,477 Views
EdwinHz
NXP TechSupport
NXP TechSupport

Hi @Daan,

First of all I must mention that the use of malloc in embedded systems is highly discouraged. As stated in this post, malloc tries to allocate memory but does not check for operation overflow. This is why you are getting a BusFault (BFAR). Please follow the recommendations that are mentioned on the previous post I sent.

BR,
Edwin.

0 Kudos
Reply

1,456 Views
Daan
Contributor III

Hi Edwin, thanks for your reply. As you can see in my code, I call malloc(), but immediately right after that, I free it again, so I don't think the memory reservation will run beyond the heap, since I also reserve just small pieces of memory. Furthermore, the NXP SDK also uses malloc, which in my opinion isn't such a bad idea, there is plenty of RAM available. I'll dive a little deeper and check the addresses malloc returns.

0 Kudos
Reply

1,509 Views
Daan
Contributor III

I found this post , we also use the cJSON library, so I thought I might face the same problem, but the hooks are still intact after the hardfault:

Daan_0-1684746155872.png

 

0 Kudos
Reply

1,489 Views
Daan
Contributor III

According to this article , a thread-safe version of malloc should be called when using multiple threads, so I modified my code to call pvPortMalloc(); initially this seemed to run stable, but unfortunately, the system crashed after 3 hours running at malloc again, since malloc is called by pvPortMalloc.

The application is configured to use heap_3.c, which has a thread-safe implementation of malloc.

0 Kudos
Reply