简体   繁体   中英

Code Analysis : CA2000 warnings

I have following code with six CA2000 warnings. and i get this warning when i do code analysis. please let me know how to overcome this warning and why i get this warning. please do help me how to clear this warning and thanks in advance.

if (e.Row.RowType == DataControlRowType.Footer)
            {
                decimal num3 = 0;
                foreach (GridViewRow gridViewRow in this.gvTax.Rows)
                {
                    Label label2 = gridViewRow.FindControl("lbltax") as Label;
                    num3 += Convert.ToDecimal(label2.Text);
                }
                int count = e.Row.Cells.Count;
                for (int i = 0; i <= count - 1; i++)
                {
                    e.Row.Cells[i].Visible = false;
                }



                TableHeaderCell tableHeaderCell = new TableHeaderCell();
                tableHeaderCell.Text = "Total Commission";
                tableHeaderCell.ColumnSpan = 1;
                tableHeaderCell.HorizontalAlign = HorizontalAlign.Center;
                tableHeaderCell.VerticalAlign = VerticalAlign.Middle;
                e.Row.Cells.AddAt(0, tableHeaderCell);

                tableHeaderCell = new TableHeaderCell();
                tableHeaderCell.Text = Math.Round(num, 2).ToString();
                tableHeaderCell.ColumnSpan = 1;
                tableHeaderCell.HorizontalAlign = HorizontalAlign.Center;
                tableHeaderCell.VerticalAlign = VerticalAlign.Middle;
                e.Row.Cells.AddAt(1, tableHeaderCell);

                tableHeaderCell = new TableHeaderCell();
                tableHeaderCell.Text = "Net Commission";
                tableHeaderCell.ColumnSpan = 1;
                tableHeaderCell.HorizontalAlign = HorizontalAlign.Center;
                tableHeaderCell.VerticalAlign = VerticalAlign.Middle;
                e.Row.Cells.AddAt(2, tableHeaderCell);

                tableHeaderCell = new TableHeaderCell();
                tableHeaderCell.Text = Math.Round(num - num3, 2).ToString();
                tableHeaderCell.ColumnSpan = 1;
                tableHeaderCell.HorizontalAlign = HorizontalAlign.Center;
                tableHeaderCell.VerticalAlign = VerticalAlign.Middle;
                e.Row.Cells.AddAt(3, tableHeaderCell);

                tableHeaderCell = new TableHeaderCell();
                tableHeaderCell.Text = "Total Deduction";
                tableHeaderCell.ColumnSpan = 1;
                tableHeaderCell.HorizontalAlign = HorizontalAlign.Center;
                tableHeaderCell.VerticalAlign = VerticalAlign.Middle;
                e.Row.Cells.AddAt(4, tableHeaderCell);

                tableHeaderCell = new TableHeaderCell();
                tableHeaderCell.Text = Math.Round(num3, 2).ToString();
                tableHeaderCell.ColumnSpan = 1;
                tableHeaderCell.HorizontalAlign = HorizontalAlign.Center;
                tableHeaderCell.VerticalAlign = VerticalAlign.Middle;
                e.Row.Cells.AddAt(5, tableHeaderCell);
            }
        }

warnings are following.

01 ) Warning 1 CA2000 : Microsoft.Reliability : In method 'Pay.gvTax_RowDataBound(object, GridViewRowEventArgs)', object 'tableHeaderCell' is not disposed along all exception paths. Call System.IDisposable.Dispose on object 'tableHeaderCell' before all references to it are out of scope.

02) Warning 2 CA2000 : Microsoft.Reliability : In method 'Pay.gvTax_RowDataBound(object, GridViewRowEventArgs)', object 'tableHeaderCell' is not disposed along all exception paths. Call System.IDisposable.Dispose on object 'tableHeaderCell' before all references to it are out of scope.

03) Warning 4 CA2000 : Microsoft.Reliability : In method 'Pay.gvTax_RowDataBound(object, GridViewRowEventArgs)', object 'tableHeaderCell' is not disposed along all exception paths. Call System.IDisposable.Dispose on object 'tableHeaderCell' before all references to it are out of scope.

04) Warning 4 CA2000 : Microsoft.Reliability : In method 'Pay.gvTax_RowDataBound(object, GridViewRowEventArgs)', object 'tableHeaderCell' is not disposed along all exception paths. Call System.IDisposable.Dispose on object 'tableHeaderCell' before all references to it are out of scope.

05) Warning 5 CA2000 : Microsoft.Reliability : In method 'PayCommission.gvCommissionTax_RowDataBound(object, GridViewRowEventArgs)', object 'tableHeaderCell' is not disposed along all exception paths. Call System.IDisposable.Dispose on object 'tableHeaderCell' before all references to it are out of scope.

06) Warning 6 CA2000 : Microsoft.Reliability : In method 'PayCommission.gvCommissionTax_RowDataBound(object, GridViewRowEventArgs)', object 'tableHeaderCell' is not disposed along all exception paths. Call System.IDisposable.Dispose on object 'tableHeaderCell' before all references to it are out of scope.

This happens because Code Analysis cannot trace that the TabeHeaderCell will be disposed of down all paths:

TableHeaderCell tableHeaderCell = new TableHeaderCell();
tableHeaderCell.Text = "Total Commission";
tableHeaderCell.ColumnSpan = 1;
tableHeaderCell.HorizontalAlign = HorizontalAlign.Center;
tableHeaderCell.VerticalAlign = VerticalAlign.Middle;
e.Row.Cells.AddAt(0, tableHeaderCell);

If an exceptions occurs between line 1 and 6, then it will leak the instance. This is an edge-case, and you can just supress it, but I find supressing CA2000 a bad habit to get into because sometimes it could be very important not to supress them - you could be hiding memory leaks.

The way I remedy this is using the following pattern:

TableHeaderCell tableHeaderCell = new TableHeaderCell();
try
{
    tableHeaderCell.Text = "Total Commission";
    tableHeaderCell.ColumnSpan = 1;
    tableHeaderCell.HorizontalAlign = HorizontalAlign.Center;
    tableHeaderCell.VerticalAlign = VerticalAlign.Middle;
    e.Row.Cells.AddAt(0, tableHeaderCell);
}
catch
{
    tableHeaderCell.Dispose();
    throw;
}

This has the side effect of making your code quite verbose, however you could refactor your code to have a method to create table header cells with the above code inside and pass in the variables:

private static void TableHeaderCell CreateTableHeaderCell(int columnSpan, string text)
{
    //Same code as above except don't add it to e.
    return tableHeaderCell;
}

Now your code actually becomes cleaner as you reduce the duplication.

The warning is displayed because you are creating an instance of TableHeaderCell. TableHeaderCell implements IDisposable and you are not disposing it. In this case the warning is probably a red herring, and you can safely ignore it (right click warning -> suppress -> In Source) as the control should dispose of all these things when it is disposed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM