AnsweredAssumed Answered

memory problems when creating/shutting down of socket

Question asked by vines on Jun 7, 2013
Latest reply on Jun 20, 2013 by vines

Hi,

 

Here is the scenario:

 

1. When I try to connect/disconnect the socket on my device a million times by sending notification to the board, everything is working fine.

2. However, for the use case mentioned below I am having a memory problem:

     1. Connect to the board

     2.Kill the application software by task manager and  not sending a close notification to the board.

     3. Re-launch the application and try to reconnect

     4. The connection is fine and I can communicate perfectly. However I saw the size of my ram to increase with this.

     5. When I repeat steps 2-4, it will come to a point that my memory is not enough and my task is giving an out of memory blocks problem. With each           passing cycle it adds 9k+ to the memory(which is the memory of socket stack below)

 

When I call RTCS_create it alloc a memory for the TCPIP:

 

500  0x10001  TCP Control Block;RTCS/TCP

4392   0x10001  TCP Tx Window;RTCS/TCP

4392   0x10001  TCP Rx Window;RTCS/TCP

148 0x10001  TCP Send Clock;RTCS/TCP


However, isn't the purpose of shutdown(handle,FLAG_CLOSE_TX) to free this memory? Correct me if I am wrong.


Here is a snippet of my code:


func()

{

//some code here

     _RTCS_socket_part_init = 4;
     _RTCS_socket_part_grow = 2;
     _RTCS_socket_part_max  = 20;


    gLocalAddr.sin_family = AF_INET;
    gLocalAddr.sin_port = getTCP_port();//TCP_PORT;
    gLocalAddr.sin_addr.s_addr = INADDR_ANY;

    gSock = socket(PF_INET, SOCK_STREAM, 0); // pag TCP, SOCK_STREAM ang gagamitin
    if (gSock == RTCS_SOCKET_ERROR)
        os_task_block();

    error = bind(gSock, &gLocalAddr, sizeof(sockaddr_in));
    if (error != RTCS_OK)
        os_task_block();
   
    if (listen(gSock, 0) != RTCS_OK)
        os_task_block();
    gPeerLen = sizeof(gPeerAddr);

    while (TRUE)
    {
        activeSock = RTCS_selectall(0);
        if (activeSock == gSock)
        {
            gChildHandle = accept(gSock, &gPeerAddr, &gPeerLen);
            if (gChildHandle != RTCS_SOCKET_ERROR)
            {
                if (error == RTCS_OK)
                {
                    if (gChildHandle != RTCS_SOCKET_ERROR)
                    {
                        os_time_delay(1000);
                        gTcpLinkUp = true;
                        while (gTcpLinkUp)
                        {
                            os_mutexlockwhileloopcheck(&Nic_mutex,5);
                            activeSock = RTCS_selectall(0);
                            if (activeSock != gChildHandle &
activeSock != 0) //this will detect if connection request done other than gChildHandle
                                gTcpLinkUp=false;
                            if (ipcfg_get_link_active(BSP_DEFAULT_ENET_DEVICE) == FALSE)
                                gTcpLinkUp = false;
                           
                            os_time_delay(100);
                            os_mutex_unlock(&Nic_mutex);
                        }
                        shutdown(gChildHandle, FLAG_CLOSE_TX);
                    }
                }
                else
                {
                    shutdown(gChildHandle, FLAG_CLOSE_TX);
                }
            }
        }
        os_time_delay(100);
    }

}


As an additional question:


There is an option to reduce the size of the socket stack by calling setsockopt. However I don't think it is working for me. The reason is because whenever the scenario discussed above happen, it recreates the whole memory below.


500      0x10001  TCP Control Block;RTCS/TCP
4392   0x10001  TCP Tx Window;RTCS/TCP
4392   0x10001  TCP Rx Window;RTCS/TCP
148     0x10001  TCP Send Clock;RTCS/TCP


I was expecting 512bytes only for both TCP Rx and Tx.Thus it should be around 1k+ only.


Here is the snippet

#define MAX_MESSAGE 512

opt_value=MAX_MESSAGE;
error = setsockopt(
gChildHandle , SOL_TCP, OPT_RBSIZE, &opt_value,2);
error = setsockopt(
gChildHandle , SOL_TCP, OPT_TBSIZE, &opt_value,2);

//status of error after this is RTCS_OK



I read somewhere to do it before RTCS_create. However, is it really possible because setsockopt function needs a handle. Before RTCS_Create it is still not created.


Thank You.

Outcomes