habcollector wrote on Monday, September 22, 2014:
Greetings,
I have been stumped by this problem and I am reaching out for assistance. Various Google searches and a review of past posts to this forum have not resulted in a solution.
I have a function that runs as part of a task. It uses two globally declared circular buffer structures (CircularBufferRight, CircularBufferLeft).
Within the function I allocate the memory, load the memory contents from a FAT FS SD Card, and use it - no problem.
The allocation occurs as thus:
init_CB(&CircularBufferLeft, CIRCULAR_BUFFER_SIZE);
init_CB(&CircularBufferRight, CIRCULAR_BUFFER_SIZE);
The problem occurs when I attempt to free the right memory. When I am finished processing and ready to leave the function I free the memory via:
free_CB(&CircularBufferLeft);
…and later via
free_CB(&CircularBufferRight);
Using:
HeapAvailable = xPortGetFreeHeapSize() for each memory allocation and freedom
I can tell that both the left and right memory are allocated. However only the left memory is released. Freeing the right memory never returns the memory to the heap.
On the very next time this function is run the firmware crashes to a hard fault when it tries to allocate the right memory (the memory that never released).
I have tried this using heap2, heap3 and heap4 - none have worked. I can really use some help - thank you for your time and consideration.
Below are some minimal specifics regarding my code and environment - as I need to I will add more, but my first thought is there is something obvious I am missing some kind person will point out.
TARGET HARDWARE: LPC1768 (M3)
RTOS: V8.0.0
DEVELOPMENT ENVIRONMENT: IAR
typedef struct
{
uint16_t Size; // MAX NUMBER OF ELEMENTS
uint16_t Start; // INDEX OF OLDEST ELEMENT
uint16_t End; // INDEX AT WHICH TO WRITE NEW ELEMENT
uint16_t *Elems; // VECTOR OF ELEMENTS
} volatile Type_CircularBuffer;
void init_CB(Type_CircularBuffer *CircularBuffer, uint16_t BufferSize)
{
CircularBuffer->Size = BufferSize + 1; // INCLUDES AN EMPTY LOCATION
CircularBuffer->Start = 0;
CircularBuffer->End = 0;
CircularBuffer->Elems = (uint16_t *)pvPortMalloc(CircularBuffer->Size);
} // END OF init_CB
void free_CB(Type_CircularBuffer *CircularBuffer)
{
vPortFree(CircularBuffer->Elems);
} // END OF free_CB