locked
Handle leak on socket create/destroy in Metro

    Question

  • I'm trying to write network application under Windows 8 Metro. My application needs to create/destroy some amount of sockets at runtime. While trying to study out WinRT's Network API, I've found following issue - my process continuously increase it's Handle count while it create/destroy sockets. To confirm that I've wrote following sample:

    task<void>( []()
    {
      // <checkpoint 1>
    
      for(int i=0; i < 200; i++)
      {
        // create socket
        DatagramSocket    ^socket = ref new DatagramSocket();
    
        // perform binding operation synchronously
        HRESULT  hr = S_OK;
        HostName ^localHostName     = ref new HostName(L"127.0.0.1");
        String   ^localServiceName  = L"10000";
    
        create_task( socket->BindEndpointAsync(localHostName, localServiceName) ).then(
          [&hr] (task<void> previousTask)
          {
            try
            {
              previousTask.get();  // get exception
            }
            catch (Exception^ exception)
            {
              hr = exception->HResult;
            }
          }).wait();
    
        ASSERT( SUCCEEDED(hr) );
    
        // close socket
        delete socket;      // C++ version of DatagramSocket.Close(), see MSDN
        socket = nullptr;
      }
    
      // <checkpoint 2>
    });

    This task is created in main App's thread. I've set 2 breakpoints at [checkpoint 1] and [checkpoint 2] and run Sysinternal's Handle utility there:

    At checkpoint 1 (before starting create/destroy loop):

    Handle type summary:
      ALPC Port       : 10
      Desktop         : 1
      Directory       : 4
      EtwRegistration : 34
      Event           : 40
      File            : 3
      IoCompletion    : 2
      Key             : 17
      Mutant          : 1
      Section         : 5
      Semaphore       : 4
      Thread          : 9
      Timer           : 7
      TpWorkerFactory : 2
      WaitCompletionPacket: 7
      WindowStation   : 2
    Total handles: 148

    At checkpoint 2 (after the loop):

    Handle type summary:
      ALPC Port       : 9
      Desktop         : 1
      Directory       : 4
      EtwRegistration : 40
      Event           : 80
      File            : 5
      IoCompletion    : 145
      Key             : 20
      Mutant          : 1
      Section         : 5
      Semaphore       : 6
      Thread          : 26
      Timer           : 291
      TpWorkerFactory : 144
      WaitCompletionPacket: 295
      WindowStation   : 2
    Total handles: 1074

    According to this, my app continuously leaks Timer, WaitCompletionPacket and other handles. (I've tried to wait some time after the loop or insert delay into the loop hoping that workers will do their work, but none help). So what's wrong with this sample and how should I create/bind/destroy socket objects? I'm using Windows 8 Consumer Preview, build 8400.

    (I've asked this question on stackoverflow but don't find the answer)


    • Edited by zakhar_s Thursday, August 16, 2012 3:49 PM
    Thursday, August 16, 2012 1:48 PM

Answers

  • After update Windows 8 Consumer Preview to Windows 8 RTM (as well as VS) problem has gone.
    • Marked as answer by zakhar_s Friday, August 17, 2012 6:18 PM
    Friday, August 17, 2012 6:17 PM