Stepping into pvPortMalloc heap 4

Values in heap 4 space

This is a follow up question to my post from yesterday. Free RTOS Version 8.2.1

The hardware receives data through an USB interface. After application begins the RTOS, this is the first memory buffer allotted:

m_ucUsbInputBufferTemp = (UINT8*)pvPortMalloc(ucNumMsgs*MAX_USB_MESSAGE_SIZE);

Soon after this memory is allotted, there are non-zero characters in the heap memory.

Here is where the garbage begins to appear inside pvPortMalloc:
if( ( pxBlock->xBlockSize - xWantedSize ) > heapMINIMUM_BLOCK_SIZE )
{
/* This block is to be split into two. Create a new
block following the number of bytes requested. The void
cast is used to prevent byte alignment warnings from the
compiler. */
pxNewBlockLink = ( void * ) ( ( ( uint8_t * ) pxBlock ) + xWantedSize );
configASSERT( ( ( ( uint32_t ) pxNewBlockLink ) & portBYTE_ALIGNMENT_MASK ) == 0 );

					/* Calculate the sizes of two blocks split from the
					single block. */
					pxNewBlockLink->xBlockSize = pxBlock->xBlockSize - xWantedSize;
					**pxBlock->xBlockSize = xWantedSize;**

** /* Insert the new block into the list of free blocks. /*
** prvInsertBlockIntoFreeList( ( pxNewBlockLink ) );**
}

I have highlighted in bold when the non-zero chars come in. The last 4 problem chars show up when stepping inside prvInsertBlockIntoFreeList( ( pxNewBlockLink ) );

Inside prvInsertBlockIntoFreeList( ( pxNewBlockLink ) );
more non-zero chars happen during:
if( ( puc + pxBlockToInsert->xBlockSize ) == ( uint8_t * ) pxIterator->pxNextFreeBlock )
{
if( pxIterator->pxNextFreeBlock != pxEnd )
{
/* Form one big block from the two blocks. /
pxBlockToInsert->xBlockSize += pxIterator->pxNextFreeBlock->xBlockSize;
pxBlockToInsert->pxNextFreeBlock = pxIterator->pxNextFreeBlock->pxNextFreeBlock;
}
else
** {
*
** pxBlockToInsert->pxNextFreeBlock = pxEnd;**
** }**
}


pvPortMalloc makes no promises that the contents of its memory will be zero. The fact that most of the memory starts as zero is just a coincidence, due to the fact that is how C works. Heap4 may return a block, or parts of blocks that were previously freed, and they will contain the data they were freed with.

If you need the blocked zeroed, you need to do it yourself after you get it, maybe using memset.

It is just like the standard function malloc() in this respect, if you need cmalloc(), you could write your own pvPortCMalloc() that calls pvPortMalloc(), and then clears the returned buffer.

Thank you for your response. I am able to clear out those bytes but hardware is not initializing. I guess there is a memory leak I need to find.

Priya