my w3wp.exe process has recently gotten an ever-expanding memory
footprint. the two most likely causes are either a memory leak in the
object library it uses, or session variables not getting cleaned up
correctly. this post is to check one thing in the object library, to
make sure that the "using" statement is being used correctly.
yes, i know that the use of error messaging is inadvisable and
unnecessary in .NET languages. we do plan to migrate this to use
exceptions in the future, however right now 90% of the consumers of
this object library are non.NET applications which cannot handle the
..NET exception classes.
public override bool LoadByID(string sID, string sConnection)
{
p_nErrorCode = 0;
p_sErrorMessage = "";
if (AuthorityID = Guid.Empty) return false;
bool bReturn = true;
using (SqlConnection oConn = new SqlConnection(sConnection))
using (SqlCommand oCommand =
this.CreateSqlCommand("parameterlist.xml", oConn))
{
try { oConn.Open(); }
catch (Exception ex)
{
p_nErrorCode = 10;
p_sErrorMessage = "Error opening DB conenction during .DBLoadByID:
" + ex.Message;
goto Failed;
}
oCommand.Parameters["@objectid"].Value = new Guid(sID);
oCommand.Parameters["@authorityid"].Value = p_gAuthorityID;
try { oCommand.ExecuteNonQuery(); }
catch (Exception ex)
{
p_nErrorCode = 10;
p_sErrorMessage = "Sql exception during LoadByID: " + ex.Message;
goto Failed;
}
if (Convert.IsDBNull(oCommand.Parameters["@rowcount"].Value))
{
p_nErrorCode = 10;
p_sErrorMessage = "Null rowcount during LoadByID.";
goto Failed;
}
int nEffect =
Convert.ToInt32(oCommand.Parameters["@rowcount"].Value);
if (nEffect == 1)
{
p_gID = new Guid(sID);
p_dtDateCreated =
GetDateTimeParameter(oCommand.Parameters["@datecreated"]);
p_dtDateModified =
GetDateTimeParameter(oCommand.Parameters["@datemodified"]);
p_gModifiedBy = (Guid)oCommand.Parameters["@modifiedby"].Value;
p_bLoaded = true;
}
else
{
p_nErrorCode = 10;
p_sErrorMessage = "Unexpected rowcount (" + nEffect + ") during
LoadByID.";
goto Failed;
}
Failed:
if (ErrorCode > 0) bReturn = false;
}
return bReturn;
}
and here is the stuff behind the function call above
"CreateSqlCommand":
public SqlCommand CreateSqlCommand(string sFileName, SqlConnection
oConn)
{
XMLCommand oXMLCommand = new XMLCommand();
SqlCommand oCommand = oXMLCommand.CreateSqlCommand(sFileName);
if (oCommand == null)
{
p_nErrorCode = oXMLCommand.ErrorCode;
p_sErrorMessage = oXMLCommand.ErrorMessage;
return null;
}
oCommand.Connection = oConn;
return oCommand;
}
and here is the stuff behind the XMLCommand.CreateSqlCommand method:
public SqlCommand CreateSqlCommand(string sFileName)
{
p_nErrorCode = 0;
p_sErrorMessage = "";
SerializedParameters oSP = null;
XmlSerializer oSerializer = new
XmlSerializer(typeof(SerializedParameters));
oSerializer.UnknownNode += new XmlNodeEventHandler(this.UnknownNode);
oSerializer.UnknownAttribute += new
XmlAttributeEventHandler(this.UnknownAttribute);
try
{
using (FileStream oFS = new FileStream(p_sXmlPath + sFileName,
FileMode.Open, FileAccess.Read, FileShare.Read))
{
oSP = (SerializedParameters) oSerializer.Deserialize(oFS);
oFS.Close();
}
}
catch(Exception ex)
{
p_nErrorCode = 10;
p_sErrorMessage = "Error accessing or serializing XML document: " +
ex.Message;
goto Failed;
}
oSerializer = null;
Failed:
if (ErrorCode > 0) return null;
return oSP.CreateSqlCommand();
}
so is there anything in here that looks like it would likely cause a
memory leak? many thanks for the scrutiny and suggestions.
jason