FIX: Memory Leak Using UDP Socket Under Windows CE 2.0

ID: Q183364

The information in this article applies to:

SYMPTOMS

Under Windows CE, a memory leak exists when you use a UDP (datagram) socket. The leak occurs when you call either the send() or sendto() API on either a connected or unconnected socket, respectively. A call causes a small leak in the kernel. Eventually, the handheld PC runs low on memory, which triggers message boxes detailing the error. As a result, other applications might terminate due to insufficient memory.

CAUSE

A kernel critical section object in the route cache of the IP layer. Calls to send/sendto on UDP sockets cause a leak per call.

RESOLUTION

There are no hot fixes available. The only viable solution is to avoid using datagram sockets and use stream sockets instead.

STATUS

Microsoft has confirmed this to be a problem in the Microsoft products listed at the beginning of this article. This problem was corrected Windows CE, version 2.1.

MORE INFORMATION

Steps to Reproduce Behavior

To reproduce the problem put the following code into a file and compile it using the winsock.lib. This sample code creates a datagram socket and sends out 1 byte of data on each call to the sendto() API.

Sample Code

   #include <windows.h>
   #include <winsock.h>
   #include <stdlib.h>
   #include <string.h>

   int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
                   LPTSTR lpCmdLine, int nCmdShow)
   {
       char    *server_name= "10.255.0.164",  // Send to arbitrary address.
                data[1];
       WSADATA  wsaData;
       SOCKET   conn_socket;
       struct   sockaddr_in server;

       WSAStartup(0x101, &wsaData);

       server.sin_addr.s_addr = inet_addr(server_name);
       server.sin_family = AF_INET;
       server.sin_port   = htons(5001);

       conn_socket = socket(AF_INET, SOCK_DGRAM, 0);

       data[0] = '0';
       while(1)
            sendto(conn_socket, data, 1, 0, (struct sockaddr *)&server,
               sizeof(server));

       closesocket(conn_socket);
       WSACleanup();
   }

Additional query words:
Keywords          : kbnetwork kbAPI kbIP kbSDKPlatform kbWinCE200bug kbWinsock kbWinCE210fix kbGrpNet 
Issue type        : kbbug
Solution Type     : kbfix

Last Reviewed: December 17, 1998