Infinite loop in heap_4..

perivr wrote on Monday, April 22, 2013:

I am using heap_4. My application allocate and free on run time. I have noticed today that code stuck at, line number 315.

code snippet starts

static void prvInsertBlockIntoFreeList( xBlockLink *pxBlockToInsert )
xBlockLink *pxIterator;
unsigned char *puc;

/* Iterate through the list until a block is found that has a higher address
than the block being inserted. */
for( pxIterator = &xStart; pxIterator->pxNextFreeBlock < pxBlockToInsert; pxIterator = pxIterator->pxNextFreeBlock )
/* Nothing to do here, just iterate to the right position. */

code snippet ends.

I have attached my debugger screen shot when this infinite loop encountered. You can see all the values in it. I have allocated heap size as 20480 bytes, on PIC24FJ256GB206. Free mem at that time was 9684 bytes. I log free memory when I free chunk to heap.

I think, these should be check, if next block is null. Please confirm.

End of post.

rtel wrote on Monday, April 22, 2013:

Most likely cause is that the data structures have somehow got corrupt, or that vPortFree() has been passed an invalid pointer.

Do you have configASSERT() defined?  Do something very simply like the following in FreeRTOSConfig.h if not:

#define configASSERT( x ) if( x == 0 ) { taskDISABLE_INTERRUPTS(); for(;;); }

just to see if it ever gets called (in this case, if it is called, your program will just sit in the infinite loop).  If it is called then you can write a more sophisticated version to find which line is causing it to be called.


perivr wrote on Monday, April 22, 2013:

Thanks Richard, for quick reply.

I have enabled assert; it is as follows,

1. In config file,
#define configASSERT( x )                       if((x)==0) vAssertCalled( __FILE__, __LINE__ )

2. In one of other C file,

void vAssertCalled( char* fileName, INT16 lineNo )
    static signed char FileName;
    memcpy( FileName, fileName, 16);
    FileName = ‘\0’;
    while(1);   // I set a breakpoint on this line, while I am in debugger.
    // return;
} // end, vAssertCalled()

Similarly, I coded for stack overflow etc. I have put break points in assert and stackoverflow. Hence, debugger should have stopped at it.

In this infinite-loop issue, none of these fences got trigger.

pflaenzchen wrote on Friday, May 03, 2013:

I have a similar problem as you Perivr. Have you already found a solution or the cause of this error?

rtel wrote on Friday, May 03, 2013:

Unfortunately it is not something I have been able to reproduce so my assumption at the moment is that there is a basic RAM corruption occurring somewhere that is trampling on the data structures.  If you are able to determine a different cause then please get back to me here.


smmg wrote on Saturday, May 25, 2013:


I have the same problem as Perivr, but running on a PIC32MX575F512H. 32768 bytes of heap, of which about 5K are still free when this happens. I am trying to allocate a block of only 96 bytes.

For testing, after I found this topic, I have compiled with heap_3.c instead of heap_4.c and I get a general exception caught with the function _general_exception_handler, but a lot later. Type is 7 - Bus error exception (data reference: load or store) and it happens as the xc32 provided malloc function is called.
heap_2.c gives the same result as heap_4.c.

No succes with configASSERT and no stack overflow ever being detected.

Any new ideas, or a solution? I am stuck right now in this project.


richard_damon wrote on Sunday, May 26, 2013:

As was mentioned, the most likely cause is that the heap has been corrupted by accessing out side the bounds of a heap object (perhaps forgetting to +1 the value of strlen when allocating blocks on the heap).

The other possibility is that the heap has been corrupted by multi-threading issues. One big one would be calling them from ISR routines (the heap_# routines are thread safe but not ISR safe). For heap_3, there is also the possible issue of using malloc/free directly in a non-threadsafe manner.

perivr wrote on Sunday, May 26, 2013:

In my case,

1. Initially, issue was very consistent.

2. Then, I went about coding features, with assumption that I will resolve issue later. Infinite loop issue become infrequent.

3. I have resolved one memory related issue recently. It was similar to Richard’s suggestion (+1 on memory allocation for string functions).

4. I haven’t seen infinite loop issue in last 2-3 days.

Lets assume that my memory related fix, will resolve infinite issue too. In case, I still see the issue, I will update this post.

Thanks & Regards.