38

Should I still call Dispose() on my socket after closing it?

For example:

mySocket.Shutdown(SocketShutdown.Both);
mySocket.Close();
mySocket.Dispose(); // Redundant?

I was wondering because the MSDN documentation says the following:

Closes the Socket connection and releases all associated resources.

Jeroen Vannevel
  • 43,651
  • 22
  • 107
  • 170
Kevin
  • 5,626
  • 3
  • 28
  • 41

6 Answers6

45

Calling Close internally calls Dispose so you don't need to call both. From .NET Reflector:

public void Close()
{
    if (s_LoggingEnabled)
    {
        Logging.Enter(Logging.Sockets, this, "Close", (string) null);
    }
    ((IDisposable)this).Dispose();
    if (s_LoggingEnabled)
    {
        Logging.Exit(Logging.Sockets, this, "Close", (string) null);
    }
}

If possible you should use the using pattern so that you always call Dispose regardless of any exceptions that might occur.

Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
  • 2
    Thnx for the dasm, but this is very strange. MSDN says Close() is a good and legitimate way to abort an asyncronous operation such as BeginReceive(), and then EndReceive would return "object disposed" error. Any idea how is it supposed to work? – Pavel Radzivilovsky Jan 11 '11 at 20:38
  • http://stackoverflow.com/questions/4662553/how-to-abort-sockets-beginreceive/4662631#4662631 – Pavel Radzivilovsky Jan 11 '11 at 22:35
  • What can you say about unreachable IP? @Mark Byers https://stackoverflow.com/questions/44694061/udpclient-unreachable-ip-connection-time-with-using-blocks – Salih Karagoz Jun 23 '17 at 13:38
23

Close and Dispose are the same in this case. When Ms introduced the Dispose pattern in .Net 1, the Dispose word was not very discoverable. So the guideline was to add context specific keyword that will do the same functionality and will be more easily discoverable by users. Like Close for files and sockets.

Alex Reitbort
  • 13,504
  • 1
  • 40
  • 61
  • 2
    Relevant MSDN blog post: [The difference between Close and Dispose](https://blogs.msdn.microsoft.com/kimhamil/2008/03/15/the-often-non-difference-between-close-and-dispose/). – Paul Apr 19 '17 at 20:23
10

By convention, you should always call Dispose on anything that implements IDisposable. You never know what other things it might do beyond the obvious.

And if you happen to use Reflector to see that, in fact, it currently isn't needed, you should not assume that the internal implementation may change at some point.

It never hurts to call Dispose. Just do it :)

Erv Walter
  • 13,737
  • 8
  • 44
  • 57
  • It doesn't HURT to call Dispose, but you don't need to if you're using the "using" construct properly; it's done automatically. The "using" construct actually compiles to a "try...finally", and Dispose is called inside "finally". – Andy Aug 30 '10 at 15:06
  • 4
    You are correct. I didn't spell it out, but "using construct" = calling Dispose. As long as Dispose is being called. In the specific case listed, it is often difficult to use the "using construct" with sockets because they are not constructed, used, and disposed in a single method call. Sockets often live longer than a single method and therefore have to have Dispose called directly. – Erv Walter Aug 30 '10 at 16:17
2

It's generally regarded as a best practice by many to close IDisposable objects, because it makes the code clearer. Explicitly calling Dispose, though, is automatically done if you encapsulate the usage of the IDisposable in a using statement, as this page describes.

Andy
  • 856
  • 9
  • 26
0

Let .NET invoke dispose:

using ( Socket mySocket = new Socket( ... ) ) {
  // Do stuff with socket, then bring it down nicely
}  // Dispose gets called returning the unmanaged system resources
Walt Stoneburner
  • 2,562
  • 4
  • 24
  • 37
  • 1
    The question isn't about what using does, but rather if he should call .Dispose() after calling .Close() (regardless of calling Dispose explicitly or not). – Bartho Bernsmann Feb 13 '14 at 22:58
0

You are right. The documentation is not clear since in the dispose method it is written that you should call Dispose() to clean the socket unmanaged resources, but on the other hand Close should do the same. I guess that Close calls Dispose or the contrary. The same behavior is supported by files so it is an educated guess.

Ikaso
  • 2,268
  • 19
  • 26
  • For some kinds of objects, `Close` might retain some resources that would permit the object to be reopened; for other kinds, `Close` would have no reason to retain any resources. In either case, since there should be no harm in calling `IDisposable.Dispose` after `Close`, doing so will ensure compatibility with both kinds of object. – supercat Feb 10 '14 at 16:37