5

I am using FreeRTOS V6.1.1 on a STM32F107VC and get frequent malloc errors. The heap area is defined in the linker script but it keeps getting stuck in this loop of pvPortMalloc() after a few allocations:

while( ( pxBlock->xBlockSize < xWantedSize ) && ( pxBlock->pxNextFreeBlock != NULL ) )
{
    pxPreviousBlock = pxBlock;
    pxBlock = pxBlock->pxNextFreeBlock;
}

pxBlock: 0x20002300
pxPreviousBlock: 0x20002300
pxNewBlockLink: 0x00
xHeapHasBeenInitialised: 0x01

linker script:

/* Entry Point */
ENTRY(Reset_Handler)

/* Highest address of the user mode stack */
_estack = 0x20010000;    /* end of 64K RAM */

/* Generate a link error if heap and stack don't fit into RAM */
_Min_Heap_Size = 0;      /* required amount of heap  */
_Min_Stack_Size = 0x200; /* required amount of stack */

/* Specify the memory areas */
MEMORY
{
  FLASH (rx)      : ORIGIN = 0x08000000, LENGTH = 256K
  RAM (xrw)       : ORIGIN = 0x20000000, LENGTH = 64K
  MEMORY_B1 (rx)  : ORIGIN = 0x60000000, LENGTH = 0K
}

...

How can this be?

glts
  • 21,808
  • 12
  • 73
  • 94
RootRaven
  • 395
  • 5
  • 14
  • Looks like a stack overflow problem, have you tried increasing the stack!? – Ishmeet Aug 21 '13 at 11:44
  • You mean the _Min_Stack_Size? I tried it now without success. This linker file was also used in larger projects and worked fine. – RootRaven Aug 21 '13 at 18:44
  • Which version of the heap are you using? Heap_1.c, heap_2.c, or heap_3.c? Some versions are fragmenting and won't work if you allocate and free blocks of memory with different sizes. – Étienne Sep 22 '13 at 16:49
  • I used heap_2, I think the ram was too fragmented. Even though the new allocations were relativly small. – RootRaven Sep 24 '13 at 12:42
  • 1
    Fragmentation is a good reason *not* to use malloc/free in real time firmware. Of course, you can use `malloc` during initialization and never call `free`, if you are using legacy code. – Mark Lakata Sep 24 '13 at 16:35
  • Indeed. But since I receive multiple serial packets in various sizes, I did not see a better alternative. – RootRaven Sep 25 '13 at 05:51
  • You could write them to a fixed (ring) buffer with some framing. Or maybe use the frtos message queues. – XTL Sep 27 '13 at 06:07
  • They are written to a fixed ring buffer but need to be parsed aferwards. Queues are not intended for variable sizes from what I read. – RootRaven Oct 04 '13 at 01:50

1 Answers1

2

This was probably caused by fragmentation in heap_2.c. Even though the allocations were pretty small, the behaviour was consistent. Using heap_4.c solved it.

RootRaven
  • 395
  • 5
  • 14
  • I'm making an application where I'll be allocating and freeing lots of blocks of memory to store dynamically generated strings of variable sizes. Based on what I learn here then is it recommendable to use heap_4.c? – m4l490n Feb 27 '14 at 17:04
  • Yes, otherwise these allocated blocks might be too small for larger strings in the future, resulting in an OutOfMemory exception even though there is enough space free. – RootRaven Mar 01 '14 at 06:51