title: ReceiveFromAsync leaking SocketAsyncEventArgs?
body: <p>I have a client application that receives video stream from a server via UDP or TCP socket.</p>
  
  <p>Originally, when it was written using .NET 2.0 the code was using BeginReceive/EndReceive and IAsyncResult. 
  The client displays each video in it's own window and also using it's own thread for communicating with the server.
  However, since the client is supposed to be up for a long period of time, and there might be 64 video streams simultaneously, there is a "memory leak" of IAsyncResult objects that are allocated each time the data receive callback is called.</p>
  
  <p>This causes the application eventually to run out of memory, because the GC can't handle releasing of the blocks in time. I verified this using VS 2010 Performance Analyzer.</p>
  
  <p>So I modified the code to use SocketAsyncEventArgs and ReceiveFromAsync (UDP case).
  However, I still see a growth in memory blocks at:</p>
  
  <pre><code>System.Net.Sockets.Socket.ReceiveFromAsync(class System.Net.Sockets.SocketAsyncEventArgs)
  </code></pre>
  
  <p>I've read all the samples and posts about implementing the code, and still no solution.</p>
  
  <p>Here's how my code looks like:</p>
  
  <pre><code>// class data members
  private byte[] m_Buffer = new byte[UInt16.MaxValue];
  private SocketAsyncEventArgs m_ReadEventArgs = null;
  private IPEndPoint m_EndPoint; // local endpoint from the caller
  </code></pre>
  
  <p><strong>Initializing</strong>:</p>
  
  <pre><code>m_Socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  m_Socket.Bind(m_EndPoint);
  m_Socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveBuffer, MAX_SOCKET_RECV_BUFFER);
  
  //
  // initalize the socket event args structure. 
  //
  m_ReadEventArgs = new SocketAsyncEventArgs();
  m_ReadEventArgs.Completed += new EventHandler&lt;SocketAsyncEventArgs&gt;(readEventArgs_Completed);
  m_ReadEventArgs.SetBuffer(m_Buffer, 0, m_Buffer.Length);
  m_ReadEventArgs.RemoteEndPoint = new IPEndPoint(IPAddress.Any, 0);
  m_ReadEventArgs.AcceptSocket = m_Socket;
  </code></pre>
  
  <p><strong>Starting the read process:</strong></p>
  
  <pre><code>bool waitForEvent = m_Socket.ReceiveFromAsync(m_ReadEventArgs);
  if (!waitForEvent)
  {
      readEventArgs_Completed(this, m_ReadEventArgs);
  }
  </code></pre>
  
  <p><strong>Read completion handler:</strong></p>
  
  <pre><code>private void readEventArgs_Completed(object sender, SocketAsyncEventArgs e)
  {
      if (e.BytesTransferred == 0 || e.SocketError != SocketError.Success)
      {
          //
          // we got error on the socket or connection was closed
          //
          Close();
          return;
       }
  
       try
       {
            // try to process a new video frame if enough data was read
            base.ProcessPacket(m_Buffer, e.Offset, e.BytesTransferred);
       }
       catch (Exception ex)
       {
            // log and error
       }
  
       bool willRaiseEvent = m_Socket.ReceiveFromAsync(e);
  
       if (!willRaiseEvent)
       {
           readEventArgs_Completed(this, e);
       }
  }
  </code></pre>
  
  <p>Basically the code works fine and I see the video streams perfectly, but this leak is a real pain.</p>
  
  <p>Did I miss anything???</p>
  
  <p>Many thanks!!!</p>