1

In the following method i get the warning:

warning : CA2000 : Microsoft.Reliability : In method 'AvdfileCache.AddPartTableGaugeData(long, long)', call System.IDisposable.Dispose on object 'cmdGaugeData' before all references to it are out of scope.

private long AddPartTableGaugeData(long lDBIdAvdFile, long lValue)
{
    long lDBIdInserted = -1;
    using (SQLiteCommand cmdGaugeData = new SQLiteCommand(_connection))
    {
        cmdGaugeData.CommandText = GetKnownSql(enuKnownSqlStatementKeys.AVDJOURN_GAUGEDATA_INSERT);
        cmdGaugeData.Parameters.AddWithValue(null, lDBIdAvdFile);
        cmdGaugeData.Parameters.AddWithValue(null, lValue);
        cmdGaugeData.ExecuteNonQuery();
        lDBIdInserted = _connection.LastInsertRowId;
    }
    return lDBIdInserted;
}

I thought because of the using-Statement it's guaranteed that cmdGaugeData gets disposed under all circumstances, so why do I still get this warning?

EDIT: by now I think this is a bug in the analyzer. I have to admit that the above code is a stripped variant of my real code (actually I have more parameters in my real code) and while trying to figure out when the warning appears i come to the following:

private long AddPartTableGaugeData(long lDBIdAvdFile, AVDPart part)
{
    long lDBIdInserted = -1;
    using (SQLiteCommand cmdGaugeData = new SQLiteCommand(_connection))
    {
        cmdGaugeData.CommandText = GetKnownSql(enuKnownSqlStatementKeys.AVDJOURN_GAUGEDATA_INSERT);

        cmdGaugeData.Parameters.AddWithValue(null, "SVal_1".ToString());
        cmdGaugeData.Parameters.AddWithValue(null, "SVal_2".ToString());
        cmdGaugeData.Parameters.AddWithValue(null, "SVal_3".ToString());
        cmdGaugeData.Parameters.AddWithValue(null, "SVal_4".ToString());
        cmdGaugeData.Parameters.AddWithValue(null, "SVal_5".ToString());
        cmdGaugeData.Parameters.AddWithValue(null, "SVal_6".ToString());
        cmdGaugeData.Parameters.AddWithValue(null, "SVal_7".ToString());
        cmdGaugeData.Parameters.AddWithValue(null, "SVal_8".ToString());
        cmdGaugeData.Parameters.AddWithValue(null, "SVal_9".ToString());
        cmdGaugeData.Parameters.AddWithValue(null, "SVal_10".ToString());
        cmdGaugeData.Parameters.AddWithValue(null, "SVal_11".ToString());
        cmdGaugeData.Parameters.AddWithValue(null, "SVal_12".ToString());
        cmdGaugeData.Parameters.AddWithValue(null, "SVal_13".ToString());
        cmdGaugeData.Parameters.AddWithValue(null, "SVal_14".ToString());
        cmdGaugeData.Parameters.AddWithValue(null, "SVal_15".ToString());
        cmdGaugeData.Parameters.AddWithValue(null, "SVal_16".ToString());
        cmdGaugeData.Parameters.AddWithValue(null, "SVal_17".ToString());
        cmdGaugeData.Parameters.AddWithValue(null, "SVal_18".ToString());
        cmdGaugeData.Parameters.AddWithValue(null, "SVal_19".ToString());
        cmdGaugeData.Parameters.AddWithValue(null, "SVal_20".ToString());
        cmdGaugeData.Parameters.AddWithValue(null, "SVal_21".ToString());
        cmdGaugeData.Parameters.AddWithValue(null, "SVal_22".ToString());
        cmdGaugeData.Parameters.AddWithValue(null, "SVal_23".ToString());
        cmdGaugeData.Parameters.AddWithValue(null, "SVal_24".ToString());
        cmdGaugeData.Parameters.AddWithValue(null, "SVal_25".ToString());
        cmdGaugeData.Parameters.AddWithValue(null, "SVal_26".ToString());
        cmdGaugeData.Parameters.AddWithValue(null, "SVal_27".ToString());
        cmdGaugeData.Parameters.AddWithValue(null, "SVal_28".ToString());
        cmdGaugeData.Parameters.AddWithValue(null, "SVal_29".ToString());
        cmdGaugeData.Parameters.AddWithValue(null, "SVal_30".ToString());

        cmdGaugeData.ExecuteNonQuery();
    }

    lDBIdInserted = _connection.LastInsertRowId;
    return lDBIdInserted;
}

This code brings the warning again, BUT:

  • No Warning, when I remove the ToString-Calls (just pass in SVal_xx as Parameter)
  • No Warning, when I use 29 time ToString, the 30th parameter with ToString generates the Warning

So any further hints on that?

suriel
  • 191
  • 1
  • 10
  • The warning is not because it won't get disposed, its saying you are calling dispose whilst you still have further references. I don't see what this is from your snippet though. Possibly disposing `SQLiteCommand` calls `Dispose` on `_connection` which you are still using in other methods on the object which contains `AddPartTableGaugeData` – Ashley Medway Feb 23 '18 at 08:55
  • read the warning correctly. you get it **because** it gets disposed although you somehow still hold a reference to to the object (or its members) – Marco Forberg Feb 23 '18 at 08:56
  • There is nothing in that code that should trigger the warning. Can you post your code, your *real* code? – nvoigt Feb 23 '18 at 11:26
  • @nvoigt the edited code was copied from visual studio and pasted here without any changes – suriel Feb 23 '18 at 11:35
  • hm, then maybe it is a bug. What version of VS do you have installed? – nvoigt Feb 23 '18 at 11:52
  • hm... i get the warning in VS2010 (Ultimate). VS2013 (Ultimate) gives no Warning – suriel Feb 23 '18 at 12:09

0 Answers0