Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# List<Stream> dispose/close

Tags:

c#

stream

I am setting up a subscription service to send reports to various people in our company on a schedule. I plan to email the reports, the reporting system I am using is able to export as PDF stream (rather than writing temp files). Most people will receive more than one report so I am trying to attach them all to one email doing something like

List<Stream> reports = new List<Stream>();
//looping code for each users set of reports
Stream stream = ReportSource.ReportDocument.ExportToStream(PortableDocFormat)
reports.Add(stream);
stream.Flush();  //unsure
stream.Close();  //unsure
//end looping code

SmtpClient smtpClient = new SmtpClient(host, port);
MailMessage message = new MailMessage(from, to, subject, body);

foreach (Stream report in reports)
{
    message.Attachments.Add(new Attachment(report, "application/pdf"));
}                
smtpClient.Send(message);

What I am unsure about is should I be flushing and closing the stream just after adding it to the list will this be ok? Or do I need to loop the List afterwards to flush and dispose? I am trying to avoid any memory leak that is possible.

like image 908
PeteT Avatar asked Dec 04 '09 16:12

PeteT


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.


4 Answers

Why not create a StreamCollection class that implements IDisposable:

public class StreamCollection : Collection<Stream>, IDisposable { }

In the Dispose method of that class, you could loop through all of the streams and properly Close/Dispose of each stream. Then your code would look like:

using (var reports = new StreamCollection())
{
   //looping code for each users set of reports
   reports.Add(ReportSource.ReportDocument.ExportToStream(PortableDocFormat));
   //end looping codeSmtpClient 

   smtpClient = new SmtpClient(host, port);
   MailMessage message = new MailMessage(from, to, subject, body);

   foreach (Stream report in reports)
   {    
      message.Attachments.Add(new Attachment(report, "application/pdf"));
   }

   smtpClient.Send(message);
}
like image 136
mkedobbs Avatar answered Oct 26 '22 14:10

mkedobbs


You could create a DisposableList that you can wrap in a using statement:

public class DisposableList<T> : List<T>, IDisposable where T : IDisposable {

    // any constructors you need...

    public void Dispose() {
        foreach (T obj in this) {
            obj.Dispose();
        }
    }
}
like image 25
thecoop Avatar answered Oct 26 '22 15:10

thecoop


Depends on if the streams are used later on when the attachment is created. I assume they are which means you'll want to dispose the streams at the end.

Remember to try-finally this. Otherwise they wont be disposed if an exception occurs.

like image 35
Quibblesome Avatar answered Oct 26 '22 15:10

Quibblesome


I do not see the logic in closing the streams right after adding it to the list. Based on the code you have provided it appears the references to those streams are being used in other places. If the streams are closed then what good do they serve?

like image 39
Brian Gideon Avatar answered Oct 26 '22 16:10

Brian Gideon