Here is some of the relevant networking code, SocketClosed() calls
socket.Close()
Anything I am doing wrong here that might be causing native memory
leaks?
public void OnClientConnect(IAsyncResult asyn)
{
Socket socket=null;
try
{
socket = m_socListener.EndAccept (asyn);
socket.SetSocketOption (SocketOptionLevel.Socket,
SocketOptionName.KeepAlive, 1);
m_socListener.BeginAccept(new AsyncCallback ( OnClientConnect
),null);
socketList.Add(socket);
updateSocketList();
textNumConnections.Text=""+socketList.Count;
WaitForData(socket,null);
}
catch(ObjectDisposedException ode)
{
if (!PRODUCTION)
{
listBoxTraffic.Items.Add("ODE Exception follows:");
listBoxTraffic.Items.Add(ode);
}
SocketClosed(socket);
}
catch(SocketException se)
{
if (!PRODUCTION)
{
listBoxTraffic.Items.Add("Socket Exception Follows:");
listBoxTraffic.Items.Add(se);
}
SocketClosed(socket);
}
}
public void WaitForData(System.Net.Sockets.Socket soc, MsgState
msgState)
{
lock(this)
{
try
{
if (msgState == null)
{
msgState = new MsgState (soc);
}
// now start to listen for any data...
soc.BeginReceive
(msgState.dataBuffer,msgState.pos,msgState.expectedSize-msgState.pos,SocketFlags.None,new
AsyncCallback (OnDataReceived),msgState);
}
catch (ObjectDisposedException ode)
{
if (!PRODUCTION)
{
listBoxTraffic.Items.Add("ODE Exception follows:");
listBoxTraffic.Items.Add(ode);
}
SocketClosed(soc);
}
catch(SocketException se)
{
if (!PRODUCTION)
{
listBoxTraffic.Items.Add("Socket Exception Follows");
listBoxTraffic.Items.Add(se);
}
SocketClosed(soc);
}
}
}
public void OnDataReceived(IAsyncResult asyn)
{
lock (this)
{
MsgState msgState=null;
try
{
msgState = (MsgState)asyn.AsyncState ;
int bytesReceived = 0 ;
bytesReceived = msgState.socket.EndReceive (asyn);
msgState.pos += bytesReceived;
//A result of 0 implies that the socket has died
if (bytesReceived == 0)
{
SocketClosed(msgState.socket);
return;
}
//Check the type of message, handle it, then wait for the next
chunk of data
msgState = processMessage(msgState);
if (msgState != null)
{
WaitForData(msgState.socket,msgState);
}
}
catch (ObjectDisposedException ode)
{
if (!PRODUCTION)
{
listBoxTraffic.Items.Add("ODE Exception follows:");
listBoxTraffic.Items.Add(ode);
}
this.SocketClosed(msgState.socket);
}
catch(SocketException se)
{
if (!PRODUCTION)
{
listBoxTraffic.Items.Add("Socket Exception follows:");
listBoxTraffic.Items.Add(se);
listBoxTraffic.Items.Add(se.ErrorCode);
listBoxTraffic.Items.Add("msg pos:"+msgState.pos+" msg
exp:"+msgState.expectedSize+" socket:"+msgState.socket);
}
this.SocketClosed(msgState.socket);
}
}
}
protected void SendData(Socket socket, byte[] data, int start, int
end)
{
try
{
socket.BeginSend(data, start, end,0,
new AsyncCallback(DataSent), socket);
}
catch (ObjectDisposedException ode)
{
if (!PRODUCTION)
{
listBoxTraffic.Items.Add("ODE Exception follows:");
listBoxTraffic.Items.Add(ode);
}
this.SocketClosed(socket);
}
catch(SocketException se)
{
if (!PRODUCTION)
{
listBoxTraffic.Items.Add("Socket Exception follows from send:");
listBoxTraffic.Items.Add(se);
listBoxTraffic.Items.Add(se.ErrorCode);
}
this.SocketClosed(socket);
}
}
protected void DataSent(IAsyncResult asyn)
{
Socket socket=null;
try {
socket = (Socket)asyn.AsyncState;
socket.EndSend(asyn);
}
catch (ObjectDisposedException ode)
{
if (!PRODUCTION)
{
listBoxTraffic.Items.Add("ODE Exception follows:");
listBoxTraffic.Items.Add(ode);
}
this.SocketClosed(socket);
}
catch(SocketException se)
{
if (!PRODUCTION)
{
listBoxTraffic.Items.Add("Socket Exception follows:");
listBoxTraffic.Items.Add(se);
listBoxTraffic.Items.Add(se.ErrorCode);
}
this.SocketClosed(socket);
}
}