hunting down a memory leak



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

.



Relevant Pages

  • RE: hunting down a memory leak
    ... it appears that the using statement near the top for your SqlConnection ... exception since there isn't likely anything further down the line that you ... > using (SqlConnection oConn = new SqlConnection(sConnection)) ... > SqlCommand oCommand = oXMLCommand.CreateSqlCommand; ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Is this good style of C++?
    ... Ioannis Vranos wrote: ... >> If I implements RAII strictly, do you mean that memory leak can be ... >> normal running or exception thrown, objects in the scope will be ... >> not be released by releasing the pointer. ...
    (comp.lang.cpp)
  • Re: many exceptions cause memory leak?
    ... This function allocates an Exception Occurrence from the heap and copies the data from Source into this new occurrence. ... Failure to deallocate the results of the Save_Occurrence function would certainly result in a memory leak. ... Final Water Mark: ...
    (comp.lang.ada)
  • Re: D2005 Code Folding
    ... then you'd have a memory leak. ... Putting a critical function before try means that we /trust/ this function to properly handle any exception after making any memory allocation that means we /expect/ it to behave like this: ... The difference here is that we are not interested in catching *any* exceptions that a call to ECS might throw, but instead, after ECS was *successfully* called and therefore critical section acquired, we need to ensure that the corresponding LCS gets called so we have to protect the code between these two calls that could raise an exception. ...
    (borland.public.delphi.non-technical)
  • Re: Western Digital MyBook versus Airport Extreme
    ... Chris Ridd wrote: ... to be okay, with the exception perhaps having a memory leak. ...
    (uk.comp.sys.mac)