Hardfault in prvAllocate TCBAndStack

Hi I am working with the cortex – m0plus controller. when we optimize the code to -O2 we are getting the hardfault during the debug. static tskTCB *prvAllocateTCBAndStack( unsigned short usStackDepth, portSTACK_TYPE *puxStackBuffer ) { tskTCB *pxNewTCB;
/* Allocate space for the TCB.  Where the memory comes from depends on
the implementation of the port malloc function. */
pxNewTCB = ( tskTCB * ) pvPortMalloc( sizeof( tskTCB ) );

if( pxNewTCB != NULL )
{
    /* Allocate space for the stack used by the task being created.
    The base of the stack memory stored in the TCB so the task can
    be deleted later if required. */
    pxNewTCB->pxStack = ( portSTACK_TYPE * ) pvPortMallocAligned( ( ( ( size_t )usStackDepth ) * sizeof( portSTACK_TYPE ) ), puxStackBuffer );

    if( pxNewTCB->pxStack == NULL )
    {
        /* Could not allocate the stack.  Delete the allocated TCB. */
        vPortFree( pxNewTCB );
        pxNewTCB = NULL;
    }
    else
    {

        /* Just to help debugging. */
        memset( pxNewTCB->pxStack, ( int ) tskSTACK_FILL_BYTE, ( size_t ) usStackDepth * sizeof( portSTACK_TYPE ) );
    }
}

return pxNewTCB;
} I am getting the hardfault in the else part…

Hardfault in prvAllocate TCBAndStack

I just tried this GCC Cortex-M0+ project with optimisations set to -O0, -O2 and -O3, and in each case the code executed as expected. http://www.freertos.org/AtmelSAMD20RTOS.html Your code appears to be crashing when it calls a library function, not when it is calling a FreeRTOS function. You could try writing your own simple memset() implementation to see if that helps. Regards.