Answered by:
Code analysis false positive?

Question
-
User-725075801 posted
Using message As New MailMessage(safeSender, New MailAddress("accounts@grasshoppersoccer.com.au", "Accounts")) With {.IsBodyHtml = True, .Subject = "New invoice"} message.Body = String.Format(Globalization.CultureInfo.CurrentCulture, "A new invoice from company is avaiable at {0}", url.ToString) Using client As New SmtpClient() client.Port = 26 client.Send(message) End Using End Using
I have the above code twice in a class with little only differeing by email, subject and body, yet code analysis tells me that i need to dispose of mailmessage in only one of them.
It woulde seem that i am disposing of mailmessage, am i missing somthing?
ThanksSunday, June 15, 2014 10:55 PM
Answers
-
User-1454326058 posted
Hi ThatsIT,
Yes, based on my test, I can reproduce that scene.
Base on this link below, we can find that for some situations the using statement is not enough to protect IDisposable objects and cause CA2000 to occur.
# CA2000: Dispose objects before losing scope
http://msdn.microsoft.com/en-us/library/ms182289.aspx
These result of code analysis could be as the reference. For the almost situations, that code is OK.
If you want to fix it. You should to dispose all references of message object. (E.g. MailAddress) For some objects (E.g. MailAddress), they haven’t the dispose method, you should implement the dispose method manually.
Thanks
Best Regards
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, June 17, 2014 5:25 AM
All replies
-
User-1454326058 posted
Hi ThatsIT,
I have the above code twice in a class with little only differeing by email, subject and bodyFor the same logic, you could have a common logic (a method with the parameters, E.g. Subject, body) in a class, then call it directly.
Thanks
Best Regards
Monday, June 16, 2014 9:46 PM -
User-725075801 posted
Thanks but thats not my problem, the problem is that code analysis reports that i need to dispose, but i have a 'end using" that should dispose. unless there is somthing I am missing.
Thanks
Monday, June 16, 2014 9:50 PM -
User-542735381 posted
Hi Starain,
I have a big problem, the solution cannot run. I checked internet, but all the suggestions are not working for me.
I remember you solved my problem before that no body alse could solved. You are the best. So this time I wish you help me again. the following is the link of my question.
http://forums.asp.net/p/1992263/5717891.aspx?p=True&t=635385534583349953&pagenum=1
Thank you very much.
Peter
Monday, June 16, 2014 10:17 PM -
User-1454326058 posted
Hi ThatsIT,
Please provide the detail steps that you analysis the code.
Base on your code, the object of message will be disposed automatically.
Thanks
Best Regards
Monday, June 16, 2014 11:06 PM -
User-725075801 posted
I am running code analysis using VS 2013 Ultimate.
Here you can see i suppressed the error message, but i like to get to the bottom of things, i am prettry sure it is a false positive.
<System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope")> Sub SendAccountsEmail(invoice As EF.Invoice, url As Uri) If invoice Is Nothing Then Throw New ArgumentNullException("invoice") End If If url Is Nothing Then Throw New ArgumentNullException("url") End If Dim safeSender As Net.Mail.MailAddress = New Net.Mail.MailAddress("xxxxxxxxxx", String.Format(Globalization.CultureInfo.CurrentCulture, "xxxxxxxxxxx")) Using message As New MailMessage(safeSender, New MailAddress("xxxxxxxxxxx", "xxxxxx")) With {.IsBodyHtml = True, .Subject = "New invoice"} message.Body = String.Format(Globalization.CultureInfo.CurrentCulture, "xxxxxxxx{0}", url.ToString) Using client As New SmtpClient() client.Port = 26 client.Send(message) End Using End Using End Sub
Thanks
Tuesday, June 17, 2014 1:39 AM -
User-1454326058 posted
Hi ThatsIT,
Yes, based on my test, I can reproduce that scene.
Base on this link below, we can find that for some situations the using statement is not enough to protect IDisposable objects and cause CA2000 to occur.
# CA2000: Dispose objects before losing scope
http://msdn.microsoft.com/en-us/library/ms182289.aspx
These result of code analysis could be as the reference. For the almost situations, that code is OK.
If you want to fix it. You should to dispose all references of message object. (E.g. MailAddress) For some objects (E.g. MailAddress), they haven’t the dispose method, you should implement the dispose method manually.
Thanks
Best Regards
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, June 17, 2014 5:25 AM