Disposing implicit references within using statements - c#

When "nesting" using statements/blocks, such as a StreamWriter within a FileStream
using (FileStream fs = File.Open(path, FileMode.Create))
{
using (var fsw = new StreamWriter(fs))
{
...
}
}
Would the FileStream be properly disposed of if it's reference is implicit? If not, would FileStream dispose of it when it itself is disposed of?
using (var fsw = new StreamWriter(File.Open(path, FileMode.Create)) )
{
...
}
Also, does the following "stacked" using statements generate differently than the first example that is "nested" (are the try/catch blocks nested or not depending on the syntax)?
using (FileStream fs = File.Open(path, FileMode.Create))
using (var fsw = new StreamWriter(fs))
{
...
}

Would the FileStream be properly disposed of if it's reference is implicit? Will FileStream dispose of it when it itself is disposed of?
The question is nonsensical. I believe the question you intended to ask is:
If the StreamWriter (or reader) is disposed (or closed), will it automatically dispose the underlying Stream?
Yes. For future reference, consider reading the documentation; your question is clearly answered there.
Now, you might reason as follows: suppose a thread abort exception is thrown after the creation of the stream but before the creation of the writer or reader. In the two-usings case, the stream is disposed; in the one-using case, it is not. Therefore the two-using case is both different and better.
That reasoning is specious. Suppose a thread abort exception is thrown after the handle for the stream is allocated but before the file stream variable is assigned; even in the two-usings case, the stream is not disposed.
The moral of the story is: you must not rely upon using to dispose of critical resources in a world with thread abort exceptions. using is for politeness; it is not a guarantee that a resource will be deallocated.
does the following "stacked" using statements generate differently than the first example that is "nested"?
Please only ask one question per question.
The question is unclear. Is your question whether the two forms are exactly semantically equivalent, or whether they generate exactly the same IL? Those questions have opposite answer. Yes, they are semantically equivalent, and no, they do not necessarily generate the same IL if optimizations are turned off.
In general,
statement
and
{
statement
}
are semantically equivalent. (Though note that a declaration statement is syntactically illegal in some contexts where a statement block is not. And of course, braces introduce a declaration space.)
However the C# compiler may choose to generate extra nop instructions so that the debugger has some place to put breakpoints associated with the braces. Thus, the code generated is not identical. Generating extra nops can cause knock-on effects on branch locations, and therefore branch offset sizes, and therefore branch instruction sizes, and therefore branch instruction opcodes, for instance.

Would the FileStream be properly disposed of if it's reference made
implicit?
Yes. The var keyword gets determined at compile time and it would result in the same code and be disposed of correctly.
Also, does the following generate differently than the first example
that is nested (are the try/catch blocks nested or not depending on
the syntax)?
There is no catch block with the using statement. The using statement is the equivalent of try finally. Depending on the compiler optimizations, the original code may result in the same as the stacked using statement. Logically, it really shouldn't matter.

Prior to .NET 4.5 the StreamWriter class assumed it owned the stream that was passed into it. So the below will close and dispose of the FileStream when the StreamWriter is closed/disposed.
using (var fsw = new StreamWriter(File.Open(path, FileMode.Create)) )
{
...
}
As of .NET 4.5 there is a StreamWriter constructor with a Boolean parameter that, when True, will leave the FileStream open.
So you have control over whether the FileStream is disposed or not.
As for stacking the using statements: the { }'s are optional when containing a single statement, just as they are for if, for and so on. Stacking the using's is a formatting convenience for those who view the indention to be excessive and unnecessary.
I would suggest following the same convention you would for other block statements when they contain a single statement to keep your code consistent.

Related

confused when using "using block" - C#

I often use the "using" block to dispose the objects.
Today, I using HttpWebRequest to post data, and I feel confused between two method.
Method 1:
var request = (HttpWebRequest)WebRequest.Create("http://www...");
using (var writer = new StreamWriter(request.GetRequestStream()))
{
writer.Write(JsonConvert.SerializeObject(content));
}
Method 2:
var request = (HttpWebRequest)WebRequest.Create("http://www...");
using (var stream = request.GetRequestStream())
using (var writer = new StreamWriter(stream))
{
writer.Write(JsonConvert.SerializeObject(content));
}
In method 2, all stream and streamwirter in "using" blocks, so certainly it will be dispose. But in method 1, I am not sure stream request.GetRequestStream() will be dispose. Can anyone explain to me? Thanks alot!
Basically, it depends on two things:
Whether the StreamWriter constructor will ever throw an exception when passed a non-null Stream reference - and I don't think it will in this case. (If the stream were read-only, it would... at which point the stream wouldn't be disposed.)
Whether StreamWriter.Dispose disposes of the underlying stream - and it does, unless you've called the constructor overload which explicitly allows you to control that
So basically, I believe it's safe in this case... when your using statement disposes of the StreamWriter, that will dispose of the Stream.
As noted in comments, however, using two using statements means you don't need to perform this sort of reasoning.
A StreamWriter wrapping a Stream will close that stream when it is closed. See the docs.
Both are the same. Since using block disposes the entire object, even anonymous objects.

Should I dispose readonly FileStream

I have a readonly FileStream which is a method local variable:
public void SomeMethod()
{
var fileStream = File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite));
... //some stream operations
}
Should I call Dispose (explicitly or by "using") at the end of the method? What will it change?
It doesn't matter what it does, IDisposable is implemented by FileStream, and so you need to call Dispose implicitly or explicitly when you've finished using it. It is part of the contract of using the class in your code.
I think it's best to wrap that in a using statement. You also need exception handling if you really want that code to be robust. It will work as it is now, it's just bad practice.
It is a must to dispose any instance implementing IDisposable and a good practice to dispose it trough using statement.
Don't think that analyzing every case in particular would help.
If you fail to dispose, the FileStream won't be closed until the GC kicks in (non-deterministically).
And until this happens, you will be holding an open file handle, preventing some types of access to the file (e.g. writing, deletion).
Note that specifying FileShare.ReadWrite won't necessarily help - if another writer attempts to open the file with FileShare.None (e.g. by calling File.OpenWrite), he won't be able to do so until you close the file.
So, yes, do close the file, with a using statement.
If an object can be disposed, you should dispose it as early as you don't need it anymore. From FileStream Class topic:
If a process terminates with part of a file locked or closes a file
that has outstanding locks, the behavior is undefined.
As everyone suggested: dispose every IDisposable, preferably with using.
Now for files there could be special case when you really want to block everyone else from accessing/modifying the file. In this case you'd still dispose file at some point, but this "some point" can be significantly later in the code/application lifetime.

Why must I Close() a file in C#?

I know this might seem silly, but why does the following code only work if I Close() the file? If I don't close the file, the entire stream is not written.
Steps:
Run this code on form load.
Close form using mouse once it is displayed.
Program terminates.
Shouldn't the file object be flushed or closed automatically when it goes out of scope? I'm new to C#, but I'm used to adding calls to Close() in C++ destructors.
// Notes: complete output is about 87KB. Without Close(), it's missing about 2KB at the end.
// Convert to png and then convert that into a base64 encoded string.
string b64img = ImageToBase64(img, ImageFormat.Png);
// Save the base64 image to a text file for more testing and external validation.
StreamWriter outfile = new StreamWriter("../../file.txt");
outfile.Write(b64img);
// If we don't close the file, windows will not write it all to disk. No idea why
// that would be.
outfile.Close();
C# doesn't have automatic deterministic cleanup. You have to be sure to call the cleanup function if you want to control when it runs. The using block is the most common way of doing this.
If you don't put in the cleanup call yourself, then cleanup will happen when the garbage collector decides the memory is needed for something else, which could be a very long time later.
using (StreamWriter outfile = new StreamWriter("../../file.txt")) {
outfile.Write(b64img);
} // everything is ok, the using block calls Dispose which closes the file
EDIT: As Harvey points out, while the cleanup will be attempted when the object gets collected, this isn't any guarantee of success. To avoid issues with circular references, the runtime makes no attempt to finalize objects in the "right" order, so the FileStream can actually already be dead by the time the StreamWriter finalizer runs and tries to flush buffered output.
If you deal in objects that need cleanup, do it explicitly, either with using (for locally-scoped usage) or by calling IDisposable.Dispose (for long-lived objects such as referents of class members).
Because Write() is buffered and the buffer is explicitly flushed by Close().
Streams are objects that "manage" or "handle" non-garbage collected resources. They (Streams) therefore implement the IDisposable interface that, when used with 'using' will make sure the non-garbage collected resources are clean up. try this:
using ( StreamWriter outfile = new StreamWriter("../../file.txt") )
{
outfile.Write(b64img);
}
Without the #Close, you can not be sure when the underlying file handle will be properly closed. Sometimes, this can be at app shutdown.
Because you are using a streamwriter and it doesn't flush the buffer until you Close() the writer. You can specify that you want the writer to flush everytime you call write by setting the AutoFlush property of the streamwriter to true.
Check out the docs. http://msdn.microsoft.com/en-us/library/system.io.streamwriter.aspx
If you want to write to a file without "closing", I would use:
System.IO.File
Operating system cache write to block devices to enable the OS to have better performance. You force a write by flushing the buffer after a write of setting the streamwriter to autoflush.
Because the C# designers were cloning Java and not C++ despite the name.
In my opinion they really missed the boat. C++ style destruction on scope exit would have been so much better.
It wouldn't even have to release the memory to be better, just automatically run the finalizer or the IDisposable method.

Method returns an IDisposable - Should I dispose of the result, even if it's not assigned to anything?

This seems like a fairly straightforward question, but I couldn't find this particular use-case after some searching around.
Suppose I have a simple method that, say, determines if a file is opened by some process. I can do this (not 100% correctly, but fairly well) with this:
public bool IsOpen(string fileName)
{
try
{
File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.None);
}
catch
{
// if an exception is thrown, the file must be opened by some other process
return true;
}
}
(obviously this isn't the best or even correct way to determine this - File.Open throws a number of different exceptions, all with different meanings, but it works for this example)
Now the File.Open call returns a FileStream, and FileStream implements IDisposable. Normally we'd want to wrap the usage of any FileStream instantiations in a using block to make sure they're disposed of properly. But what happens in the case where we don't actually assign the return value to anything? Is it still necessary to dispose of the FileStream, like so:
try
{
using (File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.None));
{ /* nop */ }
}
catch
{
return true;
}
Should I create a FileStream instance and dispose of that?
try
{
using (FileStream fs = File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.None));
}
...
Or are these totally unnecessary? Can we simply call File.Open and not assign it to anything (first code example), and let the GC dispose of it right away?
Yes, you should definitely dispose of the FileStream. Otherwise the stream will remain open and the file won't be usable until a finalizer happens to clean it up.
The important thing here is ownership: for File.Open, the caller is assumed to "own" the stream returned to it - and if you own something which implements IDisposable, it's your responsibility to dispose of it.
Compare this with the situation of Image.FromStream: in that case, you pass in a stream and the Image then assumes that it owns that stream. You mustn't close the stream yourself, in that case - you have to dispose of the image when you're done, and it will dispose of the stream.
Calling a static method which returns something disposable almost always assumes that the caller takes ownership of the resource. Ditto constructors (which are effectively static methods.)
The irony in this case is that if you don't dispose of the stream returned by File.Open, you'll have found that the file is usable - at the same time as making it unusable until some indeterminate time.
If a method returns an IDisposable, I personally would always put it in a using block. Even if I don't assign the return value to anything.
Even if you don't assign it to a variable, the disposable object is still created. Dispose is not going to be called automatically. The only difference will be that the returned object will become immediately eligible for garbage collection, because there are no (strong) references to it.
The garbage collector does not call Dispose automatically when it reclaims an object. However, most IDisposable types provide a finalizer (which will be called just before the GC reclaims an object) that invokes Dispose as a fallback strategy (safety net) — study the IDisposable pattern to see how this is done:
~SomeClass // <-- the finalizer method will usually call Dispose;
{ // but you have no control over when it will be called!
Dispose(false);
}
Remember that you don't know when the garbage collector will run (because it's non-deterministic). Therefore, you also don't know when the finalizer method will be called. And because of that -- if you haven't called Dispose explicitly (either yourself, or with a using block) -- you don't know when it will be called by the finalizer.
That's the advantage of calling Dispose explicitly: You can free resources -- or at least allow the GC to free managed resources -- as soon as you're done with them, instead of holding on to resources until the finalizer gets called sometime in the future.
Yes, you don't want to leave the FileStream opened. For one, you won't even be able to open the file yourself after that. Calling Close() is good enough, but using using is probably the preferred pattern.
There's a much bigger problem with your code however. It cannot possibly work reliably on Windows. A typical scenario:
The File.Open() call succeeds. You close it
Your thread gets pre-empted by the Windows scheduler
Another thread in another process gets a chance to run, it opens the file
Your thread regains the CPU and continues after the File.Open() call
You open the file, trusting that it will work since IsOpen() returned false.
Kaboom.
Never write code like this, failure is extremely hard to diagnose. Only ever open a file when you are ready to start reading or writing to it. And don't close it until you are done.
Extra bonus: it is now obvious that you want to use a using statement.
When you call any method that returns something, an instance of that something is being created. Just because you're not actually capturing it, doesn't make it any less "there". Therefore, in the case of an IDisposable object, the object is being created by the method you're calling in spite of the fact that you're doing nothing with it. So yes, you still need to dispose of it somehow. The first approach with the using statement seems like it should work.

using() and dispose with multiple wrapped streams

Am I right that you'd only need a using() for the outermost stream if you're doing e.g
MemoryStream mstr = new MemoryStream();
using(StreamWriter w = new StreamWriter(mstr)) {
....
}
As disposing the StreamWriter should also dispose/close the underlying stream, there's no need to do this ?:
using(MemoryStream mstr = new MemoryStream())
using(StreamWriter w = new StreamWriter(mstr)) {
....
}
(Note these are just examples, for how to dispose wrapped streams, not looking for alternatives like just use a StringWriter etc.)
It's a good idea to put all dependent resources in their own using blocks from a readability and maintainability point of view as much as anything. e.g. in the below code, after the final brace, it's impossible to attempt to access mstr because it is scoped to within the block where it is valid:
using (MemoryStream mstr = new MemoryStream())
using (StreamWriter w = new StreamWriter(mstr) {
....
}
// cannot attempt to access mstr here
If you don't scope it like this, then it's still possible to access mstr outside the scope of where it is valid
MemoryStream mstr = new MemoryStream();
using (StreamWriter w = new StreamWriter(mstr) {
....
}
mstr.Write(...); // KABOOM! ObjectDisposedException occurs here!
So while it may not always be necessary (and in this case it isn't) it's a good idea as it both clarifies and enforces your intention as to its scope.
My rule of thumb: If it implements IDisposable, dispose of it.
While currently (and probably forever), calling StreamWriter.Dispose() closes the underlying stream, other stream-derived classes you may use in the future may not. Also, it seems not to actually call Dispose() either, so non-MemoryStreams may not get properly disposed (although I can't think of any that would suffer from that right now).
So, while you can safely dispose only of the StreamWriter, I find it a much better practice to always use using blocks for disposables whenever possible.
From looking at the StreamWriter.Dispose method in Reflector, it looks like the underlying streams get closed, but not disposed. I would put each stream in a "using" block in order to explicity dispose of them all.
To answer your actual question. TextWriter's Dispose method
public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}
It calls a protected Dispose and passes true to it. From TextWriter.Dispose Method (Boolean)
When the disposing parameter is true,
this method releases all resources
held by any managed objects that this
TextWriter references. This method
invokes the Dispose method of each
referenced object.
However it is best practice to wrap everything that implements IDisposable in a using block, because we cannot guarantee that that the protected Dipose method will always be called with the true parameter.
It's really a better practice to just always use a using block.
"Always", except for the one well-known case of WCF proxy classes, where a design flaw can sometimes cause their Dispose methods to throw an exception, losing your original exception.

Categories

Resources