I'm trying to determine if there are glaring errors in a code base, or not.

The code in question calls a third party dll which returns an IDataReader. If the code uses the reader without disposing of it, it won't be explicitly returned to the pool, correct?

Here's the calling code:

IDataReader rdr = db.ExecSPGetDataReader("dbo.someStoredProcedure", paramList);
if (rdr.Read())
{
    List<nameValuePair> formValues = Utils.nameValuePairs(rdr["valuepairs"].ToString());
    foreach (nameValuePair nvp in formValues)
    {
        if (nvp.name.ToLower() == "name")
        {
            outString = nvp.value;
            break;
        }
    }
}

Here's the decompiled third party dll code:

    public IDataReader ExecSPGetDataReader(string sp, List<param> paramList)
    {
        IDataReader dataReader;
        using (DbCommand dbC = this.setDBCommand(sp, paramList, true))
        {
            IDataReader dr = this._db.ExecuteReader(dbC);
            this.setOutputParams(dbC, paramList);
            dataReader = dr;
        }
        return dataReader;
    }

It looks like the command gets disposed of, probably for the purpose of disposing the connection, but if that's true, how can anything be read from the returned IDataReader?

有帮助吗?

解决方案

If the code uses the reader without disposing of it, it won't be explicitly returned to the pool, correct?

That is correct. Change the code to this:

using (IDataReader rdr =
    db.ExecSPGetDataReader("dbo.someStoredProcedure", paramList))
{
    if (rdr.Read())
    {
        List<nameValuePair> formValues =
            Utils.nameValuePairs(rdr["valuepairs"].ToString());
        foreach (nameValuePair nvp in formValues)
        {
            if (nvp.name.ToLower() == "name")
            {
                outString = nvp.value;
                break;
            }
        }
    }
}

The using statement will ensure that Dispose is called.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top