IDispose confusion

joe_pool_is

Contributor
Joined
Jan 18, 2004
Messages
507
Location
Longview, TX [USA]
I have recreated System.Net.Mail.Message, because Microsoft did not see a reason to implement the Serializable feature. It looks like everything in Microsoft's Mail.Message class can be duplicated by organizing and naming strings appropriately.

I want to implement IDispose in my class so that it cleans itself up nicely, and (with the way I designed my class) all I need to dispose of are strings!

My email attachments are loaded from the local PC using a Stream Reader, then loaded into a string buffer (which gives me a 2 GB capacity - I'm hoping this is going to work, but it is still being developed).

My custom Message object handles attachments by creating a List of MyAttachments like so:
Code:
List<MyAttachment> Attachments

When I dispose of the resources used by my mail message model, how should I handle the strings?

Take the attachments, for example - Should I:
  • Set each String to an Empty String (i.e. attachment = string.Empty)?
  • Set the String to NULL (i.e attachment = null)?
  • Clear the List [i.e. Attachments.Clear()]?
  • A combination of above?
  • Or, are strings managed (i.e. I don't need to do anything)?

I would show the code, but I had to create a Mail Address class and an Attachment class to enable my Mail Message class to look like Microsoft's version (makes it easy to learn), so there are several lines of code and I've commented it well. Perhaps one day, I'll post it on CodeProject...

For now, here's my snippet of disposing related code:
Code:
#region IDisposable Members

~JpMessage() {
  Dispose(false);
}

public void Dispose() {
  Dispose(true);
  GC.SuppressFinalize(this);
}
// If disposing equals false, the method has been called by the 
// runtime from inside the finalizer and you should not reference 
// other objects. Only unmanaged resources can be disposed.
protected virtual void Dispose(bool disposing) {
  if (this._disposed == false) {
    if (disposing == true) {
      if (!_from.IsDisposed) _from.Dispose();
      if (!_replyTo.IsDisposed) _replyTo.Dispose();
      if (!_sender.IsDisposed) _sender.Dispose();
      foreach (JpMailAddress obj in _bcc) {
        if (!obj.IsDisposed) obj.Dispose();
      }
      foreach (JpMailAddress obj in _cc) {
        if (!obj.IsDisposed) obj.Dispose();
      }
      foreach (JpMailAddress obj in _to) {
        if (!obj.IsDisposed) obj.Dispose();
      }
      foreach (JpAttachment obj in _attachments) {
        if (!obj.IsDisposed) obj.Dispose();
      }
    }
  }
  _disposed = true;
}

#endregion
 
Last edited:
Strings will be garbage collected so you don't need to do anything with them, if you are only using strings then there is no need to implement IDisposable in your class at all.
 
OK, thanks.

I like the clean look of the using statement (must have MSG in it, because it is addictive), and I wanted to use it in my class.

To use "using" though, I had to implement IDisposable, which was new, virgin territory for me as a developer.
 
Back
Top