J
joproulx
After trying to investigate a handle leak problem in my application, I
found out this post which pretty much describes the behavior that I
have experienced:
http://www.dotnet247.com/247reference/msgs/51/255221.aspx
And here is part of my code:
private void Socket_AcceptConnectCallback(IAsyncResult ar)
{
Socket socket = (Socket)ar.AsyncState;
if( socket==null )
return;
if ( socket.Connected)
{
socket.EndConnect(ar);
// Connection succeeded
CreateNewConnection(socket, out
m_iClientMainConnection);
}
else
{
socket.Close(); // Doesn't release all the handles!!!
socket = null;
}
}
The problem is that even though I close my socket when it fails to
connect, the handle count keeps going up. From the post mentionned
above, this doesn't seem to be a leak but the way the GC manage the
lifetime of the handles. After a certain time where the handle count
can reach a really high number, the GC will release a bunch of them.
My questions are: How can you determine that you really are confronted
to a handle leak when you never know when the GC will release the
handle? Is there a way to tell the GC to always release the handle when
requested by the code for testing purpose? If I do not call Close() on
the socket, will it really leak or is the GC will eventually release
the handle?
Best regards,
Jonathan
found out this post which pretty much describes the behavior that I
have experienced:
http://www.dotnet247.com/247reference/msgs/51/255221.aspx
And here is part of my code:
private void Socket_AcceptConnectCallback(IAsyncResult ar)
{
Socket socket = (Socket)ar.AsyncState;
if( socket==null )
return;
if ( socket.Connected)
{
socket.EndConnect(ar);
// Connection succeeded
CreateNewConnection(socket, out
m_iClientMainConnection);
}
else
{
socket.Close(); // Doesn't release all the handles!!!
socket = null;
}
}
The problem is that even though I close my socket when it fails to
connect, the handle count keeps going up. From the post mentionned
above, this doesn't seem to be a leak but the way the GC manage the
lifetime of the handles. After a certain time where the handle count
can reach a really high number, the GC will release a bunch of them.
My questions are: How can you determine that you really are confronted
to a handle leak when you never know when the GC will release the
handle? Is there a way to tell the GC to always release the handle when
requested by the code for testing purpose? If I do not call Close() on
the socket, will it really leak or is the GC will eventually release
the handle?
Best regards,
Jonathan