Code inspection says I need to dispose object. Which one?
Asked Answered
O

1

17

This is my function. I already wrapped both client and message into using clause and still get error when run code inspection. Error points to first using line:

public static void Send(MailItem mail)
        {
            var sender = Membership.GetUser(mail.CreatedBy);
            if (sender == null)
            {
                return;
            }

            using (var msg = new MailMessage { From = new MailAddress(ConfigurationManager.AppSettings["EmailSender"], ConfigurationManager.AppSettings["EmailSenderName"]) })
            {
                foreach (var recipient in mail.MailRecipients)
                {
                    var recipientX = Membership.GetUser(recipient.UserKey);
                    if (recipientX == null)
                    {
                        continue;
                    }

                    msg.To.Add(new MailAddress(recipientX.Email, recipientX.UserName));
                }

                msg.Subject = "[From: " + sender.UserName + "]" + mail.Subject;
                msg.Body = mail.Body;

                if (HttpContext.Current != null)
                {
                    msg.Body += Environment.NewLine + Environment.NewLine + "To reply via Web click link below:" +
                                Environment.NewLine;
                    msg.Body += ConfigurationManager.AppSettings["MailPagePath"] + "?AID=" +
                                ContextManager.CurrentAccount.AccountId + "&RUN=" + sender.UserName;
                }

                try
                {
                    using (var emailClient = new SmtpClient())
                    {
                        emailClient.Send(msg);
                    }
                }
                catch (Exception ex)
                {
                    Logger.LogException(ex);
                }
            }
        }

This is warning I get:

Warning 1 CA2000 : Microsoft.Reliability : In method 'Email.Send(MailItem)', object '<>g_initLocal0' is not disposed along all exception paths. Call System.IDisposable.Dispose on object '<>g_initLocal0' before all references to it are out of scope. C:\CodeWorkspace\Code\Utility\Email.cs 41

Osmium answered 24/6, 2011 at 20:40 Comment(4)
Well what is at line 41 in your code?Breckenridge
@BrokenGlass: What is at line 41 in his code?Beverle
@Mehrdad: Impossible to say considering this is only part of the code within the file.Breckenridge
I said in my post that error points to the first "using" lineOsmium
F
18

Your problem is this line:

using (var msg = new MailMessage { From = new MailAddress(ConfigurationManager.AppSettings["EmailSender"], ConfigurationManager.AppSettings["EmailSenderName"]) }) 

The initializer block { From = ... } is executed after the object is constructed and before the using block's internal try/finally begins.

If the MailAddress constructor (or its argument expressions, or the assignment to From if it is a property accessor) throws an exception, the MailMessage will not be disposed.

Change to:

using (var msg = new MailMessage()) 
{
    msg.From = new MailAddress(ConfigurationManager.AppSettings["EmailSender"], ConfigurationManager.AppSettings["EmailSenderName"]);
    ...
}

The temporary <>g_initLocal0 variable is the name of the MailMessage before it gets assigned to msg.

Forras answered 24/6, 2011 at 20:54 Comment(2)
Moved creation of "From" inside block and voila!Osmium
I've encountered this before with different objects but similiar circumstances. Set the MailAddress after the MailMessage has been constructed.Milla

© 2022 - 2024 — McMap. All rights reserved.