Note that there are some explanatory texts on larger screens.

plurals
  1. POMemory leak C# Async (got stack)
    text
    copied!<p>I have a application where I switched out the communication from TCPClient to using SocketAsyncEventArgs, but I do have a problem that occures when the application been running for a few hours, and I cant seem to find it. Here is my Stack Trace, anyone got an idea?:</p> <pre><code>Framework Version: v4.0.30319 Description: The process was terminated due to an unhandled exception. Exception Info: System.OutOfMemoryException Stack: at System.Threading.ThreadPoolWorkQueue.Enqueue(System.Threading.IThreadPoolWorkItem, Boolean) at System.Threading.ThreadPool.QueueUserWorkItemHelper(System.Threading.WaitCallback, System.Object, System.Threading.StackCrawlMark ByRef, Boolean) at System.Threading.ThreadPool.QueueUserWorkItem(System.Threading.WaitCallback, System.Object) at System.IO.Ports.SerialStream+EventLoopRunner.CallEvents(Int32) at System.IO.Ports.SerialStream+EventLoopRunner.WaitForCommEvent() at System.Threading.ThreadHelper.ThreadStart_Context(System.Object) at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean) at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object) at System.Threading.ThreadHelper.ThreadStart() </code></pre> <p>Code:</p> <pre><code>public Client(Form1 parent) { todo = new ArrayList(); buffert = new List&lt;MsgStruct&gt;(); this.parent = parent; this.ip = ini.GetFromIni("CONFIG", "IP"); this.port = ini.GetFromIni("CONFIG", "PORT"); data = new byte[100000]; // Addres of the host. IPAddress[] addressList = host.AddressList; // Instantiates the endpoint and socket. this.hostEndPoint = new IPEndPoint(addressList[addressList.Length - 1], Convert.ToInt32(port)); this.clientSocket = new Socket(this.hostEndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp); startTimer = new System.Timers.Timer(); startTimer.Elapsed += new ElapsedEventHandler(startSendLoop); startTimer.Interval = 1000; startTimer.Start(); sendTimer = new System.Timers.Timer(); sendTimer.Elapsed += new ElapsedEventHandler(sendloop); sendTimer.Interval = 500; sendTimer.Start(); pingTimer = new System.Timers.Timer(); pingTimer.Elapsed += new ElapsedEventHandler(pingTimer_Elapsed); pingTimer.Interval = 13000; pingTimer.Start(); } internal void Disconnect() { try { clientSocket.Disconnect(true); } catch (Exception e) { System.Diagnostics.Debug.WriteLine(e.ToString()); MessageBox.Show(e.ToString()); } } public void Dispose() { try { autoConnectEvent.Close(); autoSendReceiveEvents[SendOperation].Close(); autoSendReceiveEvents[ReceiveOperation].Close(); if (this.clientSocket.Connected) { this.clientSocket.Close(); } } catch (Exception ex) { } } public void pingTimer_Elapsed(object sender, ElapsedEventArgs e) { sendPing = true; } public void startSendLoop(object sender, ElapsedEventArgs e) { try { if (!this.clientSocket.Connected) { connectArgs = new SocketAsyncEventArgs(); connectArgs.UserToken = this.clientSocket; connectArgs.RemoteEndPoint = this.hostEndPoint; connectArgs.Completed += new EventHandler&lt;SocketAsyncEventArgs&gt;(OnConnect); clientSocket.ConnectAsync(connectArgs); bool test = autoConnectEvent.WaitOne(5000); gotData = true; lastTime = DateTime.Now; } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.ToString()); } } private void ProcessError(SocketAsyncEventArgs e) { Socket s = e.UserToken as Socket; if (s.Connected) { // close the socket associated with the client try { s.Shutdown(SocketShutdown.Both); } catch (Exception) { // throws if client process has already closed } finally { if (s.Connected) { s.Close(); } } } } private void OnConnect(object sender, SocketAsyncEventArgs e) { try { autoConnectEvent.Set(); // Set the flag for socket connected. this.connected = (e.SocketError == SocketError.Success); } catch (Exception ex) { } } private void OnReceive(object sender, SocketAsyncEventArgs e) { try { while (true) { if (canReceive) { canReceive = false; string stringData; int recv = 0; for (int i = 0; i &lt; e.Buffer.Length; i++) { if (e.Buffer[i] != 0) recv++; else break; } if (recv &gt; 0) { int count = 0; for (int i = 0; i &lt; data.Length; i++) { if (data[i] != 0) count++; else break; } e.Buffer.CopyTo(data, count); lastTime = DateTime.Now; gotData = true; if ((byte)data[count + recv - 1] == (byte)255) { int cnt = -1; for (int i = 0; i &lt; count + recv; i++) { if (data[i] == (byte)254) { cnt = i; break; } } int nr = (count + recv) - cnt - 2; byte[] tmp = new byte[nr]; for (int i = 0; i &lt; nr; i++) { tmp[i] = data[cnt + i + 1]; } string crc = Encoding.UTF8.GetString(tmp); stringData = Encoding.UTF8.GetString(data, 0, cnt); MsgStruct msgs = new MsgStruct(stringData); msgs.setCrc(crc); todo.Add(msgs); data = new byte[100000]; } } canReceive = true; break; } else Thread.Sleep(10); } handleToDo(); autoSendReceiveEvents[SendOperation].Set(); e.Dispose(); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.ToString()); } } private void OnSend(object sender, SocketAsyncEventArgs e) { try { // Signals the end of send. sendSuccess = true; autoSendReceiveEvents[ReceiveOperation].Set(); if (e.SocketError == SocketError.Success) { if (e.LastOperation == SocketAsyncOperation.Send) { // Prepare receiving. Socket s = e.UserToken as Socket; byte[] receiveBuffer = new byte[100000]; e.SetBuffer(receiveBuffer, 0, receiveBuffer.Length); e.Completed += new EventHandler&lt;SocketAsyncEventArgs&gt;(OnReceive); s.ReceiveAsync(e); } } else { this.ProcessError(e); } } catch (Exception ex) { } } public void sendloop(object sender, ElapsedEventArgs e) { try { sendTimer.Enabled = false; if (this.clientSocket.Connected) { byte[] data = new byte[1024]; bool extendedTime = false; DateTime tmpDate = lastTime.AddSeconds(30); if (DateTime.Now &gt; tmpDate) { gotData = false; } if (canUseBuffert &amp;&amp; sendSuccess) { canUseBuffert = false; if (buffert.Count &gt; 0) { if (buffert[0] != null &amp;&amp; buffert[0].getMsg().Length != 0) { byte[] ba = Encoding.UTF8.GetBytes(buffert[0].getMsg()); if (buffert[0].getCrc() == "") { ulong tmp = CRC.calc_crc(ba, ba.Length); buffert[0].setCrc(tmp.ToString("X")); } if (buffert[0].canSendByTimeout()) { string crcStr = "?" + buffert[0].getCrc() + "?"; byte[] bb = Encoding.UTF8.GetBytes(crcStr); crcStr = Encoding.UTF8.GetString(bb); byte[] fullMsg = new byte[ba.Length + bb.Length]; bb[0] = 254; bb[bb.Length - 1] = 255; ba.CopyTo(fullMsg, 0); bb.CopyTo(fullMsg, ba.Length); string s = System.Text.UTF8Encoding.ASCII.GetString(fullMsg); this.clientSocket.NoDelay = false; completeArgs = new SocketAsyncEventArgs(); completeArgs.SetBuffer(fullMsg, 0, fullMsg.Length); completeArgs.UserToken = this.clientSocket; completeArgs.RemoteEndPoint = this.hostEndPoint; completeArgs.Completed += new EventHandler&lt;SocketAsyncEventArgs&gt;(OnSend); sendSuccess = false; // Start sending asyncronally. clientSocket.SendAsync(completeArgs); } } } else { extendedTime = true; byte[] bba = Encoding.UTF8.GetBytes("X"); this.clientSocket.NoDelay = true; completeArgs = new SocketAsyncEventArgs(); completeArgs.SetBuffer(bba, 0, bba.Length); completeArgs.UserToken = this.clientSocket; completeArgs.RemoteEndPoint = this.hostEndPoint; completeArgs.Completed += new EventHandler&lt;SocketAsyncEventArgs&gt;(OnSend); sendSuccess = false; // Start sending asyncronally. clientSocket.SendAsync(completeArgs); } } canUseBuffert = true; if (!clientSocket.Connected &amp;&amp; !gotData) Disconnect(); } sendTimer.Enabled = true; } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.ToString()); sendTimer.Enabled = true; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload