Questions tagged [ca2202]

Visual studio code analysis for managed code : Do not dispose objects multiple times. Use this tag when asking questions related to this warning.

This warning is caused when some code paths of a method might lead to attempting to dispose the same object more than once.

Official documentation states:

Cause
A method implementation contains code paths that could cause multiple calls to System.IDisposable.Dispose or a Dispose equivalent, such as a Close() method on some types, on the same object.

Rule description
A correctly implemented Dispose method can be called multiple times without throwing an exception. However, this is not guaranteed and to avoid generating a System.ObjectDisposedException you should not call Dispose more than one time on an object.

17 questions
8
votes
7 answers

C# CA2000:Dispose objects before losing scope using FileStream/XmlTextReader

I have lots of code like this: FileStream fs = File.Open(@"C:\Temp\SNB-RSS.xml", FileMode.Open); using (XmlTextReader reader = new XmlTextReader(fs)) { /* Some other code */ } This gives me the following Code Analysis warning: CA2000 :…
The Diamond Z
  • 319
  • 3
  • 10
5
votes
3 answers

How to use StringWriter and HtmlWriter together without Code Analysis warnings

I'm using .net and need to get some html text, so I thought I would use the HtmlTextWriter and StringWriter together to get the well-formed html. But despite all the different ways I write the code I still get warnings from the static code analyzer…
Jon Tindel
  • 51
  • 1
  • 3
4
votes
1 answer

CA2202 Warning for ForEach loop

The fxcop analysis gives the CA2202 warning for the following method body over the foreach line: public void LogAnalysis(IEnumerable steps, bool append = false) { if (steps != null) { StringBuilder sb = new StringBuilder(); …
mcy
  • 1,209
  • 6
  • 25
  • 37
4
votes
5 answers

Disposing of object multiple times

I have the following code, which uses a stream to open and modify an Open XML document, and then save the new binary representation of that stream: MemoryStream stream = null; try { stream = new MemoryStream(); …
Andrew Keller
  • 3,198
  • 5
  • 36
  • 51
3
votes
3 answers

Get rid of CA2202

How can I get rid of the CA2202 warning (CA2202 : Microsoft.Usage : Object 'compressedStream' can be disposed more than once in method 'Compression.InternalDecompress(byte[])'. To avoid generating a System.ObjectDisposedException you should not call…
Ambuj
  • 445
  • 1
  • 4
  • 16
2
votes
1 answer

How to deal with this SQL injection warning (CA2100)

Below is the code I got from Microsoft page: SqlCommand public static Int32 ExecuteNonQuery(String connectionString, String commandText, CommandType commandType, params SqlParameter[] parameters) { using (SqlConnection conn = new…
Toby D
  • 1,465
  • 1
  • 19
  • 31
2
votes
3 answers

How to dispose of an object that is iterated through via its Next property?

I have an object that uses some underlying native resources, and has a pointer to the next instance, which I iterate through similar to: MyObject begin = null; try { begin = GetFirst(); while (begin != null) { MyObject next =…
esac
  • 24,099
  • 38
  • 122
  • 179
1
vote
1 answer

Cannot get rid of CA2202 warning

I have read the MSDN page on this: https://msdn.microsoft.com/en-us/library/ms182334.aspx And also this SO answer: https://stackoverflow.com/a/32554589/2257227 But the following code still generates 2 CA2202 warnings for me: FileStream fileStream =…
John Darvill
  • 1,274
  • 11
  • 17
1
vote
2 answers

null-conditional operator and CA2202: Do not dispose objects multiple times

Having the following: StringWriter sw = null; try { sw = new StringWriter(); using (var xw = new XmlTextWriter(sw)) { doc.WriteTo(xw); return sw.ToString(); } } finally { sw?.Dispose(); } triggers the CA2202 (do…
1
vote
1 answer

The dreaded CA2202 warning in Visual Studio Code Analysis

I'm going to create a new thread for this as previous accepted answers no longer work as of VS2012 and up. When using nested using statements, Visual Studio code analysis gives you the annoying CA2202 Do not dispose objects multiple times, for the…
0
votes
2 answers

Still A "Multiple Dispose" Issue, Even Though Handled

In some of my projects I've been using a pair of tried-and-true data encryption/decryption methods (the encryption method is pasted below). But I have always been dogged by this nagging CA2202 warning ("Do not dispose objects multiple times"), about…
blcamp
  • 119
  • 15
0
votes
2 answers

Streamwriter CA2202: Do not dispose objects multiple times

I have a piece of code I use while debugging to write a line of information to a file. private bool appendLine(string line2Write, string fileName) { try { StreamWriter tw; using (tw =…
Mike Sr
  • 511
  • 1
  • 5
  • 15
0
votes
0 answers

CA2202 on FileStream with StreamWriter

I am using a filestream and streamwriter to write a small text on file it giving me CA2202 warning public void WritePIDToFile() { FileStream fh = null; try { fh = new…
zer09
  • 1,507
  • 2
  • 28
  • 48
0
votes
1 answer

CA2202 Warning from Code Analysis for OracleConnection disposal

We are getting the following warning from Code Analysis in Visual Studio 2010 and I'm wondering if this is a false positive that we can safely ignore or the code should be refactored to correctly dispose the object. The relevant code: public void…
rjzii
  • 14,236
  • 12
  • 79
  • 119
0
votes
1 answer

CA2202 CA2215 HttpApplication Dispose base call

I am currently getting the CA2202 (Do not dispose objects multiple times) on code that I am analysing. The warning refers to HttpApplication.Dispose method that I am overriding. I know the IDispose.Dipose method should not be virtual or be overriden…
Tom Maher
  • 1,914
  • 2
  • 23
  • 39
1
2