10

I am pretty depressed by my programming knowledge but do we really need to dispose FileStream Object ?

Reason I am asking is because code is throwing "File being used by another process" exception once in 100 cases and for a moment as if i try again(to download file using file stream) it works fine.

Please refer to this question for code.

Since it only happening once in 100 or so making me so confused and it's happening on production server so can't debug at all, but works perfectly on my development machine...

Community
  • 1
  • 1
Mathematics
  • 7,314
  • 25
  • 77
  • 152

3 Answers3

9

The general rule is to dispose everything that is disposable.

In the specific case of a FileStream, you don't need to dispose it to close the file, you only need to use the Close method.

You should however dispose of the FileStream object anyway, as it has a finalizer. This will remove the object from the finalizer queue and make it a plain object that can be garbage collected in a single pass. If you don't dispose it, the garbage collector have to run the Finalizer method, and can't collect it until later, so it will linger in memory longer.

As you should dispose the object anyway, you can just put it in a using block. That will call the Dispose method, which in turn will call the Close method, so you don't need to do that yourself:

using (System.IO.FileStream stream = System.IO.File.Create(Path + file.Name)) {
  stream.Write(document, 0, document.Length);
}
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • I suppose, you don't need to call Dispose on FileStream, see http://stackoverflow.com/questions/7524903/should-i-call-close-or-dispose-for-stream-objects FROM MSDN https://msdn.microsoft.com/en-us/library/system.io.stream.close.aspx: Stream.Close "This method calls Dispose, specifying true to release all resources. " – Tomas Kubes Oct 16 '15 at 10:42
  • @qub1n: Some classes have a `Close` method or similar that will effectively do the same as the `Dispose` method, which you can find out by reading the documentation for each class. If in doubt, it doesn't hurt to dispose disposable classes even if they have already been closed. The `using` block is a convenient way to make sure that the object is disposed properly even if there is an exception in the code. – Guffa Oct 16 '15 at 11:25
  • There is a similar situation with the `Flush` and `Close` method of a file stream as with the `Close` and `Dispose` methods. You don't need to call `Flush` before closing the stream, because the `Close` method makes sure that `Flush` is called if needed. – Guffa Oct 16 '15 at 11:25
  • It depend of task you are working at. It would better to called it, but i still need the answer. The question was "Do I need.." For example I am looking for bug in stacktrace from TeamCity. It seems that the problem is that one FileStream is not Closed. I see in source code that Close was called, but dispose was called. And I ask myself? It this the cause of the bug? No, because Close will release all resoruces and call Dispose. – Tomas Kubes Oct 16 '15 at 12:51
  • @qub1n: If the stream isn't closed, that suggests that the `Close` call actually wasn't executed. The exception that you see might not be the exception from the cause of the error but an exception that happens because of how the code handles the first exception. – Guffa Oct 16 '15 at 13:03
  • What happens if I didn't dispose of it? How can I remove the damage done? – Web Developer Apr 27 '22 at 21:11
7

Of course, you need to dispose everything that is disposable, unless you have very good reason to not to dispose.

Put everything into a using block by default. If you call Close manually this is a code smell.

usr
  • 168,620
  • 35
  • 240
  • 369
1

If a class implements IDisposable, then you should dispose it so any resources gets closed/cleaned up asap instead of relying on the garbage collector. Better yet, wrap it in a using block so it gets closed even if an exception occurs.

using (var stream = new FileStream(...))
{
   // do stuff with stream
}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197