datareader.close is called in if - else branching. How to validate datareader is actually closed usi
- by tanmay
Hi,
I have written couple of custom rules in for fxcop 1.36. I have written a code to find weather opened datareader is closed or not. But it does not check which datareader object is calling the close() method so I can't be sure if all opened datareader objs are closed!!
2nd: if I am using data reader in IF else like
if 1=2
dr = cmd.executeReader();
else
dr = cmd2.execureReader();
end if
in this case it will search for 2 datareader objects to be closed..
I am putting my code for more clarity.
public override ProblemCollection Check(Member member)
{
Method method = member as Method;
int countCatch =0;
int countErrLog = 0;
Instruction objInstr = null;
if (method != null)
{
for (int i = 0; i < method.Instructions.Count; i++)
{
objInstr = method.Instructions[i];
if (objInstr.Value != null)
{
if (objInstr.Value.ToString().Contains("System.Data.SqlClient.SqlDataReader"))
{
countCatch += 1;
}
if (countCatch>0)
{
if (objInstr.Value.ToString().Contains("System.Data.SqlClient.SqlDataReader.Close"))
{
countErrLog += 1;
}
}
}
}
}
if (countErrLog!=countCatch)
{
Resolution resolu = GetResolution(new string[] { method.ToString() });
Problems.Add(new Problem(resolu));
}
return Problems;
Thanks and regards,
Tanmay.