I have a SocketState object that I use to wrap a buffer and socket together and pass it for use with socket methods like Begin / End async. In the destructor, I have this:
~SocketState()
{
if (!_socket.Connected) return;
_socket.Shutdown(SocketShutdown.Both);
_socket.Close();
}
When it comes to Close (), I get an ObjectDisposed exception. If I comment on the Shutdown () call, I don't get an error when it gets into the Close () method. What am I doing wrong?
Edit:
I know that the IDisposable solution is similar to how my code should be laid out, but this does not actually solve my problem. It doesn't look like the destructor gets called twice, so why not call the dispose () call instead of using the destructor? I still get the same exception when you call these 2 functions in a row.
, , , 2 try . , , ( ), , .