0

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.

  • The ColumnSpan setter could throw an ArgumentOutOfRangeException, that could be the cause of the warnings. What happens if you remove those 6 assignments to it? – Dirk Jul 15 '14 at 09:41

2 Answers2

2

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.

DaveShaw
  • 52,123
  • 16
  • 112
  • 141
0

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.

Ananke
  • 1,250
  • 9
  • 11