Calling dispose() on a reference to an IDisposable - c#

I have some code to add attachments to an email. I'm adding them via the Stream overload of the Attachment class constructor. The code to do it looks like this:
List<UploadedDocument> docs = DataBroker.GetUploadedDocs(Convert.ToInt32(HttpContext.Current.Session["offer_id"].ToString()));
//no need to keep this in session
HttpContext.Current.Session["offer_id"] = null;
int counter = 1;
foreach (UploadedDocument doc in docs)
{
stream = new MemoryStream(doc.doc);
attach = new Attachment(stream, "Attachment-" + counter.ToString());
message.Attachments.Add(attach);
}
Where doc.doc is a byte array. I want to properly dispose of each attachment and stream, but I can't do it until the message has been sent, so I was thinking about just adding them to a List<Attachment> and List<Stream> and then iterating through and calling dispose.
Something like this:
List<Attachment> attachments;
List<Stream> streams;
//...
foreach(UploadedDocument doc in docs)
{
stream = new MemoryStream(doc.doc);
streams.Add(stream);
attach = new Attachment(stream,"Name");
attachments.Add(attach);
message.Attachments.Add(attach);
}
//other processing
emailClient.Send(message);
if(attachments != null)
{
foreach(Attachment attachment in attachments)
{
attachment.Dispose();
}
}
if(streams != null)
{
foreach(MemoryStream myStream in streams)
{
myStream.Dispose();
}
}
But something tells me that won't dispose them properly if there is still a reference floating around that hasn't gotten garbage collected or something. Any thoughts?

The simplest way to handle this is to just call Dispose() on the MailMessage.
MailMessage.Dispose will automatically dispose all attachments, which in turn will close/Dispose() all of the underlying streams.
//other processing
emailClient.Send(message);
message.Dispose(); // Or just wrap this entire block in a using statement

This is already implemented by MailMessage.Dispose method:
protected virtual void Dispose(bool disposing)
{
if (disposing && !this.disposed)
{
this.disposed = true;
if (this.views != null)
{
this.views.Dispose();
}
if (this.attachments != null)
{
this.attachments.Dispose();
}
if (this.bodyView != null)
{
this.bodyView.Dispose();
}
}
}
Just wrap usage of MailMessage into using statement and all resources, used by the MailMessage will be released after you leave using block:
using(var message = new MailMessage(from, to))
{
foreach (UploadedDocument doc in docs)
{
stream = new MemoryStream(doc.doc);
attach = new Attachment(stream, "Attachment-" + counter.ToString());
message.Attachments.Add(attach);
}
emailClient.Send(message);
}

There are already replies on right way (MailMessage.Dispose), so on "dispose them properly if there is still a reference...":
Dispose will (expected too) release resources at the moment of the call irrespective of who have references to the object. One of common approaches is to also have inner flag in the object that implements Dispose that will block any further calls by throwing "Object Disposed" exception.
You can (and probably already did) observe this behavior if you Dispose your streams before done using them. I.e. in your mail case you may try to dispose stream immediately after message.Attachments.Add(attach); which most likely will lead to "stream disposed" exception later during Send call.
Note that there are some objects like MemoryStream that have specially defined behavior after Dispose. I.e. MemoryStream blocks all calls except ToArray/Lenght/GetBuffer since one of the main purposes of this class is to give you resulting byte array of a stream. As side effect MemoryStream's Dispose is essentially only setting flag to block other calls (which is fine as this class does not have any native resources).

Related

How to implement video file streaming in ASP.Net MVC?

I want to implement simple video file streaming.
There is my API controller:
[HttpGet]
[Route("api/VideoContent")]
public HttpResponseMessage GetVideoContent([FromUri] string fileName)
{
if (fileName == null)
{
return new HttpResponseMessage(HttpStatusCode.BadRequest);
}
if (Request.Headers.Range != null)
{
try
{
//using (FileStream fileStream = _videoFileProvider.GetFileStream(fileName))
//{
HttpResponseMessage partialResponse = Request.CreateResponse(HttpStatusCode.PartialContent);
FileStream fileStream = _videoFileProvider.GetFileStream(fileName);
partialResponse.Content = new ByteRangeStreamContent(fileStream, Request.Headers.Range, new MediaTypeHeaderValue("video/mp4"));
return partialResponse;
//}
}
catch (Exception)
{
return new HttpResponseMessage(HttpStatusCode.InternalServerError);
}
}
return new HttpResponseMessage(HttpStatusCode.RequestedRangeNotSatisfiable);
}
This code is working, but as you see fileStream not disposed. I tried to use using block (commented lines), but this code doesn't work - in debug mode method run without exceptions, but browser shows response with 500 error code.
Where is my mistake? Why I am getting 500 Internal Server Error? How to correctly dispose file stream in my case?
AFAIK, what have you implemented for downloading content without disposing filestream is right.
As you have been using HttpResponseMessage for returning response which is automatically disposed by the framework itself after done with sending response to the client.
This has already been pointed by MSFT guy in comment of another post
If you looks at dispose method of HttpResponseMessage in source code,
protected virtual void Dispose(bool disposing)
{
// The reason for this type to implement IDisposable is
//that it contains instances of types that implement
// IDisposable (content).
if (disposing && !_disposed)
{
_disposed = true;
if (_content != null)
{
_content.Dispose();
}
}
}
You can see _content has been disposed which is of type HttpContent i.e. in your case, object of ByteRangeStreamContent set in the Content property of HttpResponseMessage.
Disposing ByteRangeStreamContent object implemented in following way :
protected override void Dispose(bool disposing)
{
Contract.Assert(_byteRangeContent != null);
if (disposing)
{
if (!_disposed)
{
_byteRangeContent.Dispose();
_content.Dispose();
_disposed = true;
}
}
base.Dispose(disposing);
}
In above Dispose method of ByteRangeStreamContent, you can see that it is disposing itself and disposing _content(in your case FileStream) as well which is stream used for creating ByteRangeStreamContent object.
I strongly believe, your implementation without disposing filestream is correct as disposing starts in sequence when done with sending response to client.

Multiple using block, is this code safe?

Code snippet is as follows
public static string ToCompressedBase64(this string text)
{
using (var memoryStream = new MemoryStream())
{
using (var gZipOutputStream = new GZipStream(memoryStream, CompressionMode.Compress))
{
using (var streamWriter = new StreamWriter(gZipOutputStream))
{
streamWriter.Write(text);
}
}
return Convert.ToBase64String(memoryStream.ToArray());
}
}
As far as I know, if class contains field which is IDisposable then It should implement IDisposable itself and take care of disposal of the owned object, so with this assumptions, after disposal of streamWriter the gZipOutputStream and memoryStream will also be disposed. But we still need not disposed memoryStream to invoke toArray() method on It.
So the question is, Is calling ToArray() method on memoryStream at the end safe?
If I understand your question correctly, you are asking if it's safe to call ToArray() on a MemoryStream after it has been disposed.
If so, then yes, it's safe. The documentation specifies:
This method works when the MemoryStream is closed.
EDIT: And in case it's not clear whether closed also means disposed, you can also look at the source code for the Dispose method (Note: The link is to Stream.Dispose() since MemoryStream doesn't override the Dispose method):
public void Dispose()
{
/* These are correct, but we'd have to fix PipeStream & NetworkStream very carefully.
Contract.Ensures(CanRead == false);
Contract.Ensures(CanWrite == false);
Contract.Ensures(CanSeek == false);
*/
Close();
}
As you can see, calling Dispose() does nothing more than calling Close().

How to return a Stream from a method, knowing it should be disposed?

I have a method that takes FileStream as input. This method is running inside a for loop.
private void UploadFile(FileStream fileStream)
{
var stream = GetFileStream();
// do things with stream
}
I have another method which creates and returns the FileStream:
private FileStream GetFileStream()
{
using(FileStream fileStream = File.Open(myFile, FileMode.Open))
{
//Do something
return fileStream;
}
}
Now the first method throws an ObjectDisposedException when I try to access the returned FileStream, probably because it is already closed since I am using "using" to properly dispose the stream.
If I don't use "using" and instead use it as follows, then the FileStream remains open and the next iteration of the loop (operating on the same file) throws an exception telling the file is already in use:
private FileStream GetFileStream()
{
FileStream fileStream = File.Open(myFile, FileMode.Open);
//Do something
return fileStream;
}
If I use a try-finally block, where I close the stream in the finally then it also throws the ObjectDisposedException.
How to effectively return file stream and close it?
When you return an IDisposable from a method, you are relegating the responsibility of disposing it to your caller. Thus, you need to declare your using block around the entire usage of the stream, which in your case presumably spans the UploadFile call.
using (var s = GetFileStream())
UploadFile(s);
The problem is that the FileStream object is disposed as soon as you exit from the GetFileStream() method, leaving it in an unusable state. As other answers already indicate, you need to remove the using block from that method and instead put the using block around any code that calls this method:
private FileStream GetFileStream()
{
FileStream fileStream = File.Open(myFile, FileMode.Open);
//Do something
return fileStream;
}
using (var stream = GetFileStream())
{
UploadFile(stream);
}
However, I want to take this a step further. You want a way to protect the stream created by your GetFileStream() from the case where a sloppy programmer might call the method without a using block, or at least somehow strongly indicate to callers that the result of this method needs to be enclosed with a using block. Therefore, I recommend this:
public class FileIO : IDisposable
{
private FileStream streamResult = null;
public FileStream CreateFileStream(string myFile)
{
streamResult = File.Open(myFile, FileMode.Open);
//Do something
return streamResult;
}
public void Dispose()
{
if (streamResult != null) streamResult.Dispose();
}
}
using (var io = FileIO())
{
var stream = io.CreateFileStream(myFile);
// loop goes here.
}
Note that you don't necessarily need to create a whole new class for this. You may already have an appropriate class for this method where you can just add the IDisposable code. The main thing is that you can use IDisposable as a signal to other programmers that this code should be wrapped with a using block.
Additionally, this sets you up to modify the class so that you could create your IDisposable object once, before the loop, and have the new class instance keep track of everything you need to dispose at the end of the loop.
If you have a method that need to return an open file stream then all callers of that method need to take responsibility for disposing of the returned stream, since it cannot dispose of the stream before returning it.
I am not sure how to read the code in the question, since the UploadFile method receives fileStream, but then creates its own stream via GetFileStream and does not use fileStream at all.
But still I have a suggestion that might solve similar problems, too.
It called the 'Factory Isolation Pattern' (from the book 'Adaptive Code via C#' by Gary McLean Hall)
The idea is to keep object creation and destruction together, but still allow using the object in a flexible way. And all it takes is a little variation of #frankmartin's original GetFileStream method, only we turn things around and instead of letting the disposable object escape, we let the "do something" in:
private void With(Action<FileStream> do)
{
using (FileStream fileStream = File.Open(myFile, FileMode.Open))
{
do(fileStream);
}
}
You can then use this method in this way:
With(fileStream => UploadFile(fileStream);
Here the user cannot forget to dispose the stream (as #oɔɯǝɹ pointed out), in fact the user does even need know that it has to be disposed or taken care of in any special way ...

Why can't I get rid of a CA2000 warning?

I'm writing a method that resets a logging system. I need to get an instance of a CsvFileLogWriter (a custom class) and pass it to the reset method. CsvFileLogWriter is disposable so I get a CA2000 warning tell me:
Warning 2 CA2000 : Microsoft.Reliability : In method 'Logger.InitializeCsvLogger
(string)', call System.IDisposable.Dispose on object 'tempWriter'
before all references to it are out of scope.
I've followed the instructions relating to CA2000 and I end up with the following method. However, I still get the CA2000 warning.
public static void InitializeCsvLogger(string path)
{
ILogWriter tempWriter = null;
try
{
tempWriter = new CsvFileLogWriter(path);
ResetWriter(tempWriter);
tempWriter = null;
}
finally
{
if (tempWriter != null)
tempWriter.Dispose();
}
}
Can someone please spot my mistake?
EDIT
I do not wish to dispose of the writer that is reference by tempWriter - this is not a temporary object, just a temporary reference. I only dispose of it if there is a failure within the try block (so tempWriter never gets set to null and the if-statement in the finally block clears up the resources.) I do not want tempWriter disposing of unless this failure occurs - the object itself must remain in use after being set in a property by ResetWriter(tempWriter). This is as per the CA2000 rules - see http://msdn.microsoft.com/en-us/library/ms182289.aspx?queryresult=true
For clarification, here is what ResetWriter does - Writer is a static property. The method disposes the old writer and sets the new one.
private static void ResetWriter(ILogWriter newWriter)
{
if (Writer != null)
Writer.Dispose();
Writer = newWriter;
}
EDIT
I think as SLaks stated that it's a false positive. If I take the contents of ResetWriter and put them in place of the call to ResetWriter (essentially reversing an Extract Method refactoring) the CA2000 goes away.
Or in other words, the following does not give the CA2000 warning:
public static void InitializeCsvLogger(string path)
{
ILogWriter tempWriter = null;
try
{
tempWriter = new CsvFileLogWriter(path);
if (Writer != null)
Writer.Dispose();
Writer = tempWriter;
tempWriter = null;
}
finally
{
if (tempWriter != null)
tempWriter.Dispose();
}
}
When you assign null to tempWriter:
tempWriter = null;
tempWriter no longer refers to the object you created. Therefore, there's no way for you to Dispose the object.
You should really use a using block in this case instead:
using(var tempWriter = new CsvFileLogWriter(path))
{
ResetWriter(tempWriter);
}
By doing that, you no longer have to worry about calling Dispose (or setting the reference to null).
This warning is a false positive.
The Code Analysis engine doesn't realize that ResetWriter needs the writer to stay alive, so it wants you to dispose it in all circumstances.
You should suppress the warning.
By writing tempWriter = null you're preventing it from getting disposed, since the finally block only runs after that.
You should use the using statement instead.
This answer is correct, but contradicts your actual intentions.

Should I call Close() or Dispose() for stream objects?

Classes such as Stream, StreamReader, StreamWriter etc implements IDisposable interface. That means, we can call Dispose() method on objects of these classes. They've also defined a public method called Close(). Now that confuses me, as to what should I call once I'm done with objects? What if I call both?
My current code is this:
using (Stream responseStream = response.GetResponseStream())
{
using (StreamReader reader = new StreamReader(responseStream))
{
using (StreamWriter writer = new StreamWriter(filename))
{
int chunkSize = 1024;
while (!reader.EndOfStream)
{
char[] buffer = new char[chunkSize];
int count = reader.Read(buffer, 0, chunkSize);
if (count != 0)
{
writer.Write(buffer, 0, count);
}
}
writer.Close();
}
reader.Close();
}
}
As you see, I've written using() constructs, which automatically call Dispose() method on each object. But I also call Close() methods. Is it right?
Please suggest me the best practices when using stream objects. :-)
MSDN example doesn't use using() constructs, and call Close() method:
How to: Download Files with FTP
Is it good?
A quick jump into Reflector.NET shows that the Close() method on StreamWriter is:
public override void Close()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}
And StreamReader is:
public override void Close()
{
this.Dispose(true);
}
The Dispose(bool disposing) override in StreamReader is:
protected override void Dispose(bool disposing)
{
try
{
if ((this.Closable && disposing) && (this.stream != null))
{
this.stream.Close();
}
}
finally
{
if (this.Closable && (this.stream != null))
{
this.stream = null;
/* deleted for brevity */
base.Dispose(disposing);
}
}
}
The StreamWriter method is similar.
So, reading the code it is clear that that you can call Close() & Dispose() on streams as often as you like and in any order. It won't change the behaviour in any way.
So it comes down to whether or not it is more readable to use Dispose(), Close() and/or using ( ... ) { ... }.
My personal preference is that using ( ... ) { ... } should always be used when possible as it helps you to "not run with scissors".
But, while this helps correctness, it does reduce readability. In C# we already have plethora of closing curly braces so how do we know which one actually performs the close on the stream?
So I think it is best to do this:
using (var stream = ...)
{
/* code */
stream.Close();
}
It doesn't affect the behaviour of the code, but it does aid readability.
No, you shouldn't call those methods manually. At the end of the using block the Dispose() method is automatically called which will take care to free unmanaged resources (at least for standard .NET BCL classes such as streams, readers/writers, ...). So you could also write your code like this:
using (Stream responseStream = response.GetResponseStream())
using (StreamReader reader = new StreamReader(responseStream))
using (StreamWriter writer = new StreamWriter(filename))
{
int chunkSize = 1024;
while (!reader.EndOfStream)
{
char[] buffer = new char[chunkSize];
int count = reader.Read(buffer, 0, chunkSize);
if (count != 0)
{
writer.Write(buffer, 0, count);
}
}
}
The Close() method calls Dispose().
The documentation says that these two methods are equivalent:
StreamReader.Close: This implementation of Close calls the Dispose method passing a true value.
StreamWriter.Close:
This implementation of Close calls the Dispose method passing a true value.
Stream.Close: This method calls Dispose, specifying true to release all resources.
So, both of these are equally valid:
/* Option 1, implicitly calling Dispose */
using (StreamWriter writer = new StreamWriter(filename)) {
// do something
}
/* Option 2, explicitly calling Close */
StreamWriter writer = new StreamWriter(filename)
try {
// do something
}
finally {
writer.Close();
}
Personally, I would stick with the first option, since it contains less "noise".
For what it's worth, the source code for Stream.Close explains why there are two methods:
// Stream used to require that all cleanup logic went into Close(),
// which was thought up before we invented IDisposable. However, we
// need to follow the IDisposable pattern so that users can write
// sensible subclasses without needing to inspect all their base
// classes, and without worrying about version brittleness, from a
// base class switching to the Dispose pattern. We're moving
// Stream to the Dispose(bool) pattern - that's where all subclasses
// should put their cleanup now.
In short, Close is only there because it predates Dispose, and it can't be deleted for compatibility reasons.
This is an old question, but you can now(C# 8.0) write using statements without needing to block each one. They will be disposed of in reverse order when the containing block is finished.
using var responseStream = response.GetResponseStream();
using var reader = new StreamReader(responseStream);
using var writer = new StreamWriter(filename);
int chunkSize = 1024;
while (!reader.EndOfStream)
{
char[] buffer = new char[chunkSize];
int count = reader.Read(buffer, 0, chunkSize);
if (count != 0)
{
writer.Write(buffer, 0, count);
}
}
https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-8.0/using
On many classes which support both Close() and Dispose() methods, the two calls would be equivalent. On some classes, however, it is possible to re-open an object which has been closed. Some such classes may keep some resources alive after a Close, in order to permit reopening; others may not keep any resources alive on Close(), but might set a flag on Dispose() to explicitly forbid re-opening.
The contract for IDisposable.Dispose explicitly requires that calling it on an object which will never be used again will be at worst harmless, so I would recommend calling either IDisposable.Dispose or a method called Dispose() on every IDisposable object, whether or not one also calls Close().
Just to complement other answers, as of C# 8.0, you don't need to open a block of code just to use an using statement
if (...)
{
using FileStream f = new FileStream(#"C:\users\jaredpar\using.md");
// statements
}
// Equivalent to
if (...)
{
using (FileStream f = new FileStream(#"C:\users\jaredpar\using.md"))
{
// statements
}
}
docs:
https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-8.0/using

Categories

Resources