Queue/malloc/pointer...whats wrong this this?

tpham3783 wrote on Friday, June 05, 2009:


I am trying to get freertos queue/messaging to work.
As an experiment, i create a task that dynamicly allocate
memory, copy a string to this memory sector; and post to
a queue by pointer.  The default webserver demo task reads
all the elements in this queue, output to the webpage, and
free the memory block which was allocated on the other task.

here is the code, please advise on what i did wrong.  thank you.

char * Element;

xQueueHandle prvMainQueue;

static void vTaskTest(void *pvParameters)
   /* The parameters are not used. */
   ( void ) pvParameters;

   for( ;; )
        // post a message every 3 sec.
    char *tmp ;
        tmp = malloc(10);
        if(tmp != NULL){

} */

int main( void )
    /* Setup the ports. */

    prvMainQueue = xQueueCreate(10,sizeof(Element));

    /* Setup lwIP. */

    /* Create the lwIP task.  This uses the lwIP RTOS abstraction layer.*/
    sys_thread_new( vBasicWEBServer, ( void * ) NULL, mainWEBSERVER_PRIORITY );

    xTaskCreate(vTaskTest, ( signed portCHAR * ) "Test", configMINIMAL_STACK_SIZE * 2, NULL, 0, NULL );

    /* Start the check task - which is defined in this file. */
    xTaskCreate( vErrorChecks, ( signed portCHAR * ) "Check",  configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );

    /* Finally, start the scheduler.

    NOTE : Tasks run in system mode and the scheduler runs in Supervisor mode.
    The processor MUST be in supervisor mode when vTaskStartScheduler is
    called.  The demo applications included in the FreeRTOS.org download switch
    to supervisor mode prior to main being called.  If you are not using one of
    these demo application projects then ensure Supervisor mode is used here. */

    /* Should never get here! */
    return 0;

static void vProcessConnection( struct netconn *pxNetCon )
static portCHAR cDynamicPage[ webMAX_PAGE_SIZE ], cPageHits[ 11 ];
struct netbuf *pxRxBuffer;
portCHAR *pcRxString;
unsigned portSHORT usLength;
static unsigned portLONG ulPageHits = 0;

    /* We expect to immediately get data. */
    pxRxBuffer = netconn_recv( pxNetCon );

    if( pxRxBuffer != NULL )
        /* Where is the data? */
        netbuf_data( pxRxBuffer, ( void * ) &pcRxString, &usLength );

        /* Is this a GET?  We don’t handle anything else. */
        if( !strncmp( pcRxString, “GET”, 3 ) )
            pcRxString = cDynamicPage;

            /* Update the hit count. */
            sprintf( cPageHits, "%lu", ulPageHits );

            /* Write out the HTTP OK header. */
            netconn_write(pxNetCon, webHTTP_OK, (u16_t)strlen( webHTTP_OK ), NETCONN_COPY );

            /* Generate the dynamic page…

            … First the page header. */
            strcpy( cDynamicPage, webHTML_START );
            /* … Then the hit count… */
            strcat( cDynamicPage, cPageHits );
            strcat( cDynamicPage, "<p><pre>Task          State  Priority  Stack    #<br>************************************************<br>" );
            /* … Then the list of tasks and their status… */
            vTaskList( ( signed portCHAR * ) cDynamicPage + strlen( cDynamicPage ) );

            char *e;
                            strcat( cDynamicPage,e);

            /* … Finally the page footer. */
            strcat( cDynamicPage, webHTML_END );

            /* Write out the dynamically generated page. */
            netconn_write(pxNetCon, cDynamicPage, (u16_t)strlen( cDynamicPage ), NETCONN_COPY );

        netbuf_delete( pxRxBuffer );

    netconn_close( pxNetCon );

davedoors wrote on Friday, June 05, 2009:

I think you need to cut down the number of things you are doing at once. I cannot see anything wrong with the queue usage, but suspect you will see problems because you are blocking and waiting in the middle of processing a TCP frame which could cause strange timing effects at the TCP protocol level.

Try getting the queues working in a simpler example (without the WEB server) first.

tpham3783 wrote on Friday, June 05, 2009:

the code above output weird characters on the html webpage every three seconds,
so i assume that the webservice task is working properly.
one thing that can possibly go wrong would be the call to strcpy, which did not actually replicate the data.

i also tried a simplier example which output to a serial port instead of a webpage, and that too did not work.

please help. 

i am stuck on this matter for days.

rtel wrote on Friday, June 05, 2009:

I just tried this http://pastebin.com/m5594b168 and it worked as expected (real mode x86 OpenWatcom).


tpham3783 wrote on Friday, June 05, 2009:


Thank you for your help.

I changed malloc to pvportmalloc and free to vportfree and it worked.
I guess using the ported API is important, not the generic C API.

**I can’t believe I struggled 2-3 days over something as simple as this.

thanks again.