13

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 all exception paths) in the *.Designer.cs files that use the charting. The Designer files are generating pretty much all the charting code, and almost all the charting elements implement IDisposable. I have "Suppress results from generated code" checked in the project preferences, but it still does it.

Is there any way to fix this, without having to manually create the chart objects, and without disabling Code Analysis for the rest of the code in that class? Is there a way to disable it for all .Designer.cs files? Or, is there a solution to remove these warnings correctly by making the designer code take care of disposal?

drharris
  • 11,194
  • 5
  • 43
  • 56
  • see this thread http://stackoverflow.com/questions/4164928/how-to-suppress-code-analysis-on-generated-code, ithas some good idea's even though they are aimed at FxCop the person "XMLForDummies" says to try the same with Code Analysis, its worth a look/try. – Jeremy Thompson Aug 12 '11 at 06:31
  • Since this is already in a Designer.cs file, it already has those little things built in. Apparently VS Code Analysis even checks Designer files, which to me is a bit absurd; it seems like they should be the ones fixing those problems, not me! Thanks for the link though; sounds like I'm not the only one getting some bad code analysis results. – drharris Aug 12 '11 at 17:21
  • You should be able to globally suppress it in the global suppressions, file, no? – BrainSlugs83 Jul 01 '16 at 19:01
  • You can, but then it would also disable it from any of my custom code, which I have control of and should fix. I cannot fix Designer.cs autogenerated code without breaking Designer. There should be an option to turn off all CA warnings just from autogenerated code that I have no control over. – drharris Aug 16 '16 at 19:24

4 Answers4

2

A fair few developers appear to have encountered this without a luck, so +1 for a good question!

A possible solution is to write a method that override's CA2000 and suppresses the rule if the warning is detected in a designer file, here's a good start:

Writing Custom Code Analysis Rules in Visual Studio 2010

Otherwise see the comments at the end of this thread, MSFT engineers mention to log a Connect call: http://blogs.msdn.com/b/codeanalysis/archive/2010/03/22/what-s-new-in-code-analysis-for-visual-studio-2010.aspx

Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
  • Wow, I really don't want to learn how to use Introspection, but it's looking more and more like this could be the only solution. I'll likely accept this one if nobody else pipes up in the next day or so. Still hoping for that elusive checkbox nested somewhere deep in the options. :) – drharris Aug 15 '11 at 15:57
  • 1
    Ok, so it's looking like it won't be too bad. I hate to put a bunch of code in just to fix something that should work out of the box, but it's better than littering each build with 30-40 warnings. Still haven't fully figured out how to accomplish what I need to do, but these links should get me far enough to experiment with the rest. Thanks again! – drharris Aug 16 '11 at 16:10
1

I know I'm late to this but here goes.

I'm guessing these warnings are all emitted for code within the InitializeComponent method? If so then have you considered modifying the template files located in Common7\IDE\ItemTemplates folder? You could add the GeneratedCode attribute on the method in those. Since the attribute will be set only on it, all your other code within the same class will still get checked by code analysis.

Here's an example for Form designer file:

namespace $rootnamespace$
{
    partial class $safeitemrootname$
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        [System.CodeDom.Compiler.GeneratedCode("Windows Form Designer generated code", "1.0.0.0"), System.Diagnostics.DebuggerNonUserCode()]
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.Text = "$safeitemrootname$";
        }

        #endregion
    }
}
Crono
  • 10,211
  • 6
  • 43
  • 75
  • But... we'd have to make this change on everyone on our team's environment, right? -- Otherwise `[GeneratedCode]` attribute will appear and disappear at random... that seems really painful... -- also, why wouldn't we just do it to the whole class? – BrainSlugs83 Jul 01 '16 at 19:00
  • It won't disappear. Templates are only used for document creation. Once the file is created, it can be shared as it is, like any other code file. If multiple co-workers must create files then yes, each of them would need the template. But it's a one-time thing, it shouldn't be a big deal. – Crono Jul 01 '16 at 19:15
  • Also, putting the Generated attribute on the class would disable warnings for every members within it. I doubt OP would want that. – Crono Jul 01 '16 at 19:16
  • The designer only writes into the InitializeComponent method. It won't touch anything else in the file. – Crono Jul 01 '16 at 19:21
  • @Crono It's not true that the designer only touches code in the `InitializeComponent`-method. It also creates member variables. These also have to be removed from code inspection. Are there any ideas arround that? – Sebastian Schumann Aug 10 '16 at 11:37
  • @Verarind You're right, designer will create member variables. However these would only be the declarations and should not be causing CA2000 warning to show up. – Crono Aug 10 '16 at 12:19
  • @Crono Yes but it shows 1591 for missing XML comments :-( – Sebastian Schumann Aug 10 '16 at 13:40
  • @Verarind That shouldn't happen... unless if you're making your designer-generated class members public, which IMHO brings a whole other flagrance of code smells. :) – Crono Aug 10 '16 at 14:13
  • @Crono I maked it protected to derive other forms :-( No problem `#pragma warning disable 1591` as first line in designer.cs solves my problem. – Sebastian Schumann Aug 10 '16 at 14:17
1

Simply add a [SuppressMessage("Microsoft.Usage", "CA2213:DisposableFieldsShouldBeDisposed", MessageId = "..."] to the Dispose method in your *.Designer.cs file.

I just did, and I've found out that VS 2012 is clever enough to keep it there even when rewriting the file when something was changed in the designer.

Dejan
  • 9,150
  • 8
  • 69
  • 117
  • I no longer even have access to the original codebase to verify this, but it seems like it may work now. I think I remember trying this back in VS2010, and it wouldn't retain the suppression if you change something, so maybe this is a bug they fixed. Hopefully useful to others looking at this issue in the future! – drharris Apr 15 '15 at 15:08
  • I certainly remember struggling with this in earlier versions of VS so I almost didn't even try. But it seems to work. I hope, it works in all cases. Otherwise, we'll hear about it here. – Dejan Apr 16 '15 at 20:02
0

Have you tried toggling the "Suppress results from generated code" property value to true in the Code Analysis property page for your project(s)? This option is the standard mechanism for ignoring problems in generated code.

That said, generated code is code that will be executed, so ignoring its violations is not necessarily a great idea. Given the "noisiness" of CA2000, you may wish to consider disabling the rule instead.

Nicole Calinoiu
  • 20,843
  • 2
  • 44
  • 49
  • 2
    I put in my first paragraph that I had checked that with no improvement. Apparently in order for that to work, the `GeneratedCodeAttribute` needs to be set. The problem is that since Designer.cs files are partial classes, I'm betting that attribute wouldn't work for just the designer code (i.e. it would apply to my in-class code, which is unacceptable). Turning it off is really not an option... CA2000 has saved my rear more than a few times due to potential file handle locking and similar issues; my code is too I/O intensive to do that. Hoping for a way to just reduce the noise from MSChart. – drharris Aug 10 '11 at 02:04