Quality RTOS & Embedded Software

 Real time embedded FreeRTOS RSS feed 
Real time embedded FreeRTOS mailing list 
Quick Start Supported MCUs PDF Books Trace Tools Ecosystem TCP & FAT Training




Loading

FreeRTPS+TCP memory leak

Posted by qwlimited on August 10, 2017

Hello ,

I am new to FreeRTOS and +TCP, I create a simple echo server, almost every time when I connect/disconnect from echo server, there are 616 bytes gone.

echo heap: 26328
echo heap: 25712
echo heap: 25712
echo heap: 25712
echo heap: 25712
echo heap: 25096
echo heap: 25096
echo heap: 25096
echo heap: 25096
echo heap: 24480

My code as following, many thanks for your kind help

Kevin

/-----------------------------------------------------------------------------------/ void vTcpEchoTask(void *arg) { struct freertossockaddr xClient, xBindAddress; Sockett xListeningSocket, xConnectedSocket; socklent xSize = sizeof( xClient ); static const TickTypet xReceiveTimeOut = portMAXDELAY; const BaseTypet xBacklog = 1; static char cRxedData[ 128 ]; BaseType_t lBytesReceived;

/* Attempt to open the socket. */
xListeningSocket = FreeRTOS_socket( FREERTOS_AF_INET,
                                    FREERTOS_SOCK_STREAM,  /* SOCK_STREAM for TCP. */
                                    FREERTOS_IPPROTO_TCP );

/* Check the socket was created. */
configASSERT( xListeningSocket != FREERTOS_INVALID_SOCKET );

/* If FREERTOS_SO_RCVBUF or FREERTOS_SO_SNDBUF are to be used with
FreeRTOS_setsockopt() to change the buffer sizes from their default then do
it here!.  (see the FreeRTOS_setsockopt() documentation. */

/* If ipconfigUSE_TCP_WIN is set to 1 and FREERTOS_SO_WIN_PROPERTIES is to
be used with FreeRTOS_setsockopt() to change the sliding window size from
its default then do it here! (see the FreeRTOS_setsockopt()
documentation. */

/* Set a time out so accept() will just wait for a connection. */
FreeRTOS_setsockopt( xListeningSocket,
                     0,
                     FREERTOS_SO_RCVTIMEO,
                     &xReceiveTimeOut,
                     sizeof( xReceiveTimeOut ) );

/* Set the listening port to 7. */
xBindAddress.sin_port = ( uint16_t ) 7;
xBindAddress.sin_port = FreeRTOS_htons( xBindAddress.sin_port );

/* Bind the socket to the port that the client RTOS task will send to. */
FreeRTOS_bind( xListeningSocket, &xBindAddress, sizeof( xBindAddress ) );

/* Set the socket into a listening state so it can accept connections.
The maximum number of simultaneous connections is limited to 20. */
FreeRTOS_listen( xListeningSocket, xBacklog );

for( ;; )
{
    /* Wait for incoming connections. */
    xConnectedSocket = FreeRTOS_accept( xListeningSocket, &xClient, &xSize );
    configASSERT( xConnectedSocket != FREERTOS_INVALID_SOCKET );

printf("echo heap: %d n", xPortGetFreeHeapSize() ); for(; ;) { /* Receive another block of data into the cRxedData buffer. */ lBytesReceived = FreeRTOS_recv( xConnectedSocket, &cRxedData, 128, 0 );

        if( lBytesReceived > 0 )
        {
            /* Data was received, process it here. */
            //prvPorcessData( cRxedData, lBytesReceived );

            FreeRTOS_send( /* The socket being sent to. */
                                        xConnectedSocket,
                                        /* The data being sent. */
                                        &( cRxedData[ 0 ] ),
                                        /* The remaining length of data to send. */
                                        lBytesReceived,
                                        /*ulFlags. */
                                        0 );
        }
        else if( lBytesReceived == 0 )
        {
            /* No data was received, but FreeRTOS_recv() did not return an error.
            Timeout? */
        }
        else
        {
            /* Error (maybe the connected socket already shut down the socket?).
            Attempt graceful shutdown. */
            FreeRTOS_shutdown( xConnectedSocket, FREERTOS_SHUT_RDWR );
            break;
        }
    } //tcp_receive loop


    /* The RTOS task will get here if an error is received on a read.  Ensure the
    socket has shut down (indicated by FreeRTOS_recv() returning a FREERTOS_EINVAL
    error before closing the socket). */

    while( FreeRTOS_recv( xConnectedSocket, cRxedData, lBytesReceived, 0 ) >= 0 )
    {
        /* Wait for shutdown to complete.  If a receive block time is used then
        this delay will not be necessary as FreeRTOS_recv() will place the RTOS task
        into the Blocked state anyway. */
        osDelay(250  );

        /* Note - real applications should implement a timeout here, not just
        loop forever. */
    }

    /* Shutdown is complete and the socket can be safely closed. */
    FreeRTOS_closesocket( xConnectedSocket );
}

/* Must not drop off the end of the RTOS task - delete the RTOS task. */

osThreadTerminate(NULL); }


FreeRTPS+TCP memory leak

Posted by rtel on August 11, 2017

Have you tried using the echo server that is provided in the labs download?


FreeRTPS+TCP memory leak

Posted by heinbali01 on August 11, 2017

Hi Kevin, I'm wondering what object uses 616 bytes.

We are not aware of any memory leak in the library.

The above code is a very simple demo, and it has one potential problem: while it is handling a client, a new client may connect to xListeningSocket. Connecting means that memory will be allocated to create a new socket. This might add up to 616 bytes ( socket space, a new event group ).

Correction: the xBacklog parameter has been set to 1, meaning that only a single client may connect at a time.

In this case, it would be helpful if you can trace all calls to pvPortMalloc() and vPortFree(), see who is claiming 616 bytes. Note that this may also exist of a few allocations ( X + Y + Z )

Please report back when you found the cause of the memory leak


FreeRTPS+TCP memory leak

Posted by qwlimited on August 11, 2017

Yes, I copied that code as shown above


FreeRTPS+TCP memory leak

Posted by qwlimited on August 11, 2017

Thanks Hein, I will check and get back to you


FreeRTPS+TCP memory leak

Posted by qwlimited on August 14, 2017

Good morning Hein,

I checked it's my problem in one of our calculation module, very sorry !

Best regards, Kevin


FreeRTPS+TCP memory leak

Posted by heinbali01 on August 15, 2017

No problem, thank you very much for taking the effort to report back.


[ Back to the top ]    [ About FreeRTOS ]    [ Sitemap ]    [ ]




Copyright (C) 2004-2010 Richard Barry. Copyright (C) 2010-2016 Real Time Engineers Ltd.
Any and all data, files, source code, html content and documentation included in the FreeRTOSTM distribution or available on this site are the exclusive property of Real Time Engineers Ltd.. See the files license.txt (included in the distribution) and this copyright notice for more information. FreeRTOSTM and FreeRTOS.orgTM are trade marks of Real Time Engineers Ltd.

Latest News:

FreeRTOS V9.0.0 is now available for download.


Free TCP/IP and file system demos for the RTOS


Sponsored Links

⇓ Now With No Code Size Limit! ⇓
⇑ Free Download Without Registering ⇑


FreeRTOS Partners

ARM Connected RTOS partner for all ARM microcontroller cores

Renesas Electronics Gold Alliance RTOS Partner.jpg

Microchip Premier RTOS Partner

RTOS partner of NXP for all NXP ARM microcontrollers

Atmel RTOS partner supporting ARM Cortex-M3 and AVR32 microcontrollers

STMicro RTOS partner supporting ARM7, ARM Cortex-M3, ARM Cortex-M4 and ARM Cortex-M0

Xilinx Microblaze and Zynq partner

Silicon Labs low power RTOS partner

Altera RTOS partner for Nios II and Cortex-A9 SoC

Freescale Alliance RTOS Member supporting ARM and ColdFire microcontrollers

Infineon ARM Cortex-M microcontrollers

Texas Instruments MCU Developer Network RTOS partner for ARM and MSP430 microcontrollers

Cypress RTOS partner supporting ARM Cortex-M3

Fujitsu RTOS partner supporting ARM Cortex-M3 and FM3

Microsemi (previously Actel) RTOS partner supporting ARM Cortex-M3

Atollic Partner

IAR Partner

Keil ARM Partner

Embedded Artists