Questions tagged [ca2000]

25 questions
18
votes
4 answers

CA2000 when Returning Disposable Object from Method

I have a factory method that builds objects that implement IDisposable. Ultimately it is the callers that manage the lifetime of the created objects. This design is triggering a bunch of CA2000 errors. Is there something fundamentally incorrect…
Brian Triplett
  • 3,462
  • 6
  • 35
  • 61
13
votes
4 answers

Disabling/Fixing Code Analysis warnings from .Designer.cs files

I am using DataVisualization.Charting.Chart extensively, and for the most part it is working. However, I've been running Code Analysis frequently, and have all my own warnings taken care of. But, there are about 30 CA2000 (object not disposed along…
drharris
  • 11,194
  • 5
  • 43
  • 56
11
votes
2 answers

CA2000 passing object reference to base constructor in C#

I receive a warning when I run some code through Visual Studio's Code Analysis utility which I'm not sure how to resolve. Perhaps someone here has come across a similar issue, resolved it, and is willing to share their insight. I'm programming a…
Timothy
  • 4,630
  • 8
  • 40
  • 68
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
7
votes
1 answer

CA2000: object not disposed along all exception paths

Although topic has been discussed here before, but the proposed solutions don't seem to work.. I have a button-click-callback method in my form application, that shows a folder picker dialog: private void ButtonSelectReporterFolderClick(object…
Efrain
  • 3,248
  • 4
  • 34
  • 61
6
votes
3 answers

How to fix a CA2000 IDisposable C# compiler warning, when using a global cache

CA2000 is a warning regarding the IDisposable interface: CA2000 : Microsoft.Reliability : In method 'ImportProcessor.GetContext(string)', call System.IDisposable.Dispose on object 'c' before all references to it are out of scope. My method is…
Dominic Zukiewicz
  • 8,258
  • 8
  • 43
  • 61
5
votes
1 answer

C# Code Analysis CA2000

I have a function which I thought I had fixed the CA2000 warning in Code Analysis for, but it just won't go away. The warning is on SqlCommand. Here's the function: protected internal void LogUserSession(int? managerID) { using (var sqlCommand =…
Hungry Beast
  • 3,677
  • 7
  • 49
  • 75
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
5
votes
4 answers

Proper Way to Dispose: object not disposed along all exception paths

I get this message for line 84 and line 85 (the two, stacked using lines): CA2000 : Microsoft.Reliability : In method 'RavenDataAccess.GetRavenDatabase()', object '<>g_initLocal9' is not disposed along all exception paths. Call…
Bob Horn
  • 33,387
  • 34
  • 113
  • 219
4
votes
1 answer

C# CA2000 Dispose Object Before Losing Scope

This is my code, it gives me CA2000 on "new DataTable()..." and "new DataColumn()..."s usersDS.Tables.Add(new DataTable() { TableName = "Users", Columns = { new DataColumn() { ColumnName = "Handle", DataType = typeof(string) }, new…
Sinros
  • 181
  • 1
  • 3
  • 9
3
votes
1 answer

How do you deal with CA2000 (Dispose of IDisposable Objects) when the objects are placed in a Dependency Injection container?

According to Microsoft, the best practice for an HttpClient is to maintain a singleton version of an HttpClient (paraphrasing, but that's the upshot. Don't dispose of it immediately). My own testing has show that there are definite advantages to a…
Quark Soup
  • 4,272
  • 3
  • 42
  • 74
2
votes
3 answers

Why can't I get rid of a CA2000 warning?

I'm writing a method that resets a logging system. I need to get an instance of a CsvFileLogWriter (a custom class) and pass it to the reset method. CsvFileLogWriter is disposable so I get a CA2000 warning tell me: Warning 2 CA2000 :…
Richard
  • 697
  • 1
  • 7
  • 14
2
votes
1 answer

Exception filter causes CA2000 despite a using statement

The following code is a simplified extract from part of our production code. It calculates the SHA256 hash for a file and returns it as a string, or returns null if the file cannot be accessed: private static string CalculateHash(string fileName) { …
Alsty
  • 817
  • 10
  • 23
2
votes
1 answer

How to cache a DataSet and NOT receive the CA2000 warning

I have a DataSet that I would like to cache in the ASP.NET's web cache. The content does not change very often and a cache of a few hours should work. My problem is that when I create the DataSet, I get a CA2000 warning about dispose needing to be…
Peter Rilling
  • 323
  • 3
  • 7
1
vote
1 answer

NDepend rule for "Dispose objects before losing scope"

I'm evaluating NDepend as a part of an effort to enforce code quality and correct framework usage, and I am looking for a way to write the equivalent of CA2000: Dispose objects before losing scope. Anyone else has tried to do this, or has knowledge…
1
2