You may see memory leak when running ICMP multithread application on post-Vista OS


Symptoms


If you are developing ICMP multithread application, you may notice private bytes growing steadily and constantly when running on post-Vista OS, including Vista, Window Server 2008 and Windows 7.

For example:

int _tmain(int argc, _TCHAR* argv[])
{

      while(true){
  
           _beginthread(SendPing, 0, NULL);
           Sleep(50);
      }
      return 0;
}

void  SendPing(LPVOID lpParameter)
{
      HANDLE hIcmpFile;              // Handle for ICMP echo requests
      BOOL bRetVal;
 
      hIcmpFile = IcmpCreateFile();
 
      if (hIcmpFile == INVALID_HANDLE_VALUE)
      {
            //Print message
      }
      else
      {
           // close the echo request file handle
           bRetVal = IcmpCloseHandle(hIcmpFile);
          if(bRetVal)
          {
               //print message
          }
          else
          {
               //print message
          }
     }
 
     _endthread();

}

 

Cause


There is a bug in IcmpCreateFile/IcmpCloseHandle function with post-Vista OS, including Vista, Win2K8 and Win7. The growth of memory usage is due to leak of critical section object. We verified the memory leak issue does NOT occur or XP/Win2K3 due to different implement.

Resolution


The bug has been report and confirmed by Product Group.

A workaround is to add one more IcmpCreateFile call in the function as below:

int _tmain(int argc, _TCHAR* argv[])
{
      HANDLE hIcmpFile = IcmpCreateFile(); //Add this line
      while(true){
           _beginthread(SendPing, 0, NULL);
           Sleep(50);
      }
      return 0;
}