Q
Qin Zhou
This is possible memory leak can be reproduced by the
following code. I am hoping someone can help me out with
simple solutions! Thanks in advanced!
using System;
using System.IO;
using System.Xml;
using System.Xml.Schema;
using System.Xml.XPath;
using System.Xml.Xsl;
using System.Text;
using System.Security.Policy;
using System.Reflection;
namespace MemoryLeak
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class MemoryLeak
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: Add code to start application here
//
string str=null;
MemoryLeak memLeak = new MemoryLeak();
try
{
string strXml, strXslt;
strXml = memLeak.ReadWholeFile(args[0]);
strXslt = memLeak.ReadWholeFile(args[1]);
str = memLeak.TranslateXMLContent(strXml, strXslt);
}
catch (Exception e)
{
Console.WriteLine("catch..."+e.Message);
}
finally
{
Console.WriteLine(str);
}
Console.ReadLine();
}
public MemoryLeak()
{
}
public string ReadWholeFile(string fileName)
{
StreamReader sr;
char[] buffer;
int ret;
sr = new StreamReader(fileName);
buffer = new char[sr.BaseStream.Length];
ret = sr.Read(buffer, 0, (int)sr.BaseStream.Length);
sr.Close();
return new String(buffer);
}
public string TranslateXMLContent(string strXml, string
strXslt)
{
string str = null;
XmlTextReader myXsltReader = null;
XmlTextReader myXmlReader = null;
StringBuilder myStringBuilder = null;
StringWriter myStringWriter = null;
XmlTextWriter myXmlWriter = null;
Evidence evd;
//Create the XslTransform and load the stylesheet.
XslTransform xslt = new XslTransform();
try
{
evd = Assembly.GetCallingAssembly().Evidence;
myXsltReader = new XmlTextReader(new StringReader
(strXslt));
//urlResolver.Credentials =
CredentialCache.DefaultCredentials;
xslt.Load(myXsltReader, null, evd);
//Load the XML data file.
myXmlReader = new XmlTextReader(new StringReader
(strXml));
XPathDocument doc = new XPathDocument(myXmlReader);
//Add an object to convert the book price.
myStringBuilder = new StringBuilder();
myStringWriter = new StringWriter(myStringBuilder);
myXmlWriter = new XmlTextWriter(myStringWriter);
xslt.Transform(doc, null, myXmlWriter, null);
//Transform.
str = myStringBuilder.ToString();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
throw e;
}
finally
{
if (myXsltReader != null)
myXsltReader.Close();
if (myXmlReader != null)
myXmlReader.Close();
if (myStringWriter!= null)
myStringWriter.Close();
}
return str;
}
}
}
xsl file :
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:nqxsl="urn:netquote-com:xslt">
<msxsl:script language="CSharp" implements-prefix="nqxsl">
<![CDATA[
public string UpperCase(string str) {
return str.ToUpper();
}
]]>
</msxsl:script>
<xslutput method="xml" indent="no" />
<xsl:template match="/">
<xsl:value-of select="nqxsl:UpperCase(.)"/>
</xsl:template>
</xsl:stylesheet>
sample xml file:
<?xml version="1.0" encoding="UTF-8"?>
<xmlRoot>test for memory leak</xmlRoot>
There is always one Evidence Class that has a strong handle
(using windbg.exe). I know this is cased by the C# code in
the xsl file. If you take the C# out everything works
fine.
I am wonder does anybody know how to solve this issue for
me? My program converts hunderds of this kind file
everyday and it causes big memory leak. I know the
extension obj will work fine but I don't want to go that
rounte at this poing.
Many thanks
Qin Zhou
following code. I am hoping someone can help me out with
simple solutions! Thanks in advanced!
using System;
using System.IO;
using System.Xml;
using System.Xml.Schema;
using System.Xml.XPath;
using System.Xml.Xsl;
using System.Text;
using System.Security.Policy;
using System.Reflection;
namespace MemoryLeak
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class MemoryLeak
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: Add code to start application here
//
string str=null;
MemoryLeak memLeak = new MemoryLeak();
try
{
string strXml, strXslt;
strXml = memLeak.ReadWholeFile(args[0]);
strXslt = memLeak.ReadWholeFile(args[1]);
str = memLeak.TranslateXMLContent(strXml, strXslt);
}
catch (Exception e)
{
Console.WriteLine("catch..."+e.Message);
}
finally
{
Console.WriteLine(str);
}
Console.ReadLine();
}
public MemoryLeak()
{
}
public string ReadWholeFile(string fileName)
{
StreamReader sr;
char[] buffer;
int ret;
sr = new StreamReader(fileName);
buffer = new char[sr.BaseStream.Length];
ret = sr.Read(buffer, 0, (int)sr.BaseStream.Length);
sr.Close();
return new String(buffer);
}
public string TranslateXMLContent(string strXml, string
strXslt)
{
string str = null;
XmlTextReader myXsltReader = null;
XmlTextReader myXmlReader = null;
StringBuilder myStringBuilder = null;
StringWriter myStringWriter = null;
XmlTextWriter myXmlWriter = null;
Evidence evd;
//Create the XslTransform and load the stylesheet.
XslTransform xslt = new XslTransform();
try
{
evd = Assembly.GetCallingAssembly().Evidence;
myXsltReader = new XmlTextReader(new StringReader
(strXslt));
//urlResolver.Credentials =
CredentialCache.DefaultCredentials;
xslt.Load(myXsltReader, null, evd);
//Load the XML data file.
myXmlReader = new XmlTextReader(new StringReader
(strXml));
XPathDocument doc = new XPathDocument(myXmlReader);
//Add an object to convert the book price.
myStringBuilder = new StringBuilder();
myStringWriter = new StringWriter(myStringBuilder);
myXmlWriter = new XmlTextWriter(myStringWriter);
xslt.Transform(doc, null, myXmlWriter, null);
//Transform.
str = myStringBuilder.ToString();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
throw e;
}
finally
{
if (myXsltReader != null)
myXsltReader.Close();
if (myXmlReader != null)
myXmlReader.Close();
if (myStringWriter!= null)
myStringWriter.Close();
}
return str;
}
}
}
xsl file :
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:nqxsl="urn:netquote-com:xslt">
<msxsl:script language="CSharp" implements-prefix="nqxsl">
<![CDATA[
public string UpperCase(string str) {
return str.ToUpper();
}
]]>
</msxsl:script>
<xslutput method="xml" indent="no" />
<xsl:template match="/">
<xsl:value-of select="nqxsl:UpperCase(.)"/>
</xsl:template>
</xsl:stylesheet>
sample xml file:
<?xml version="1.0" encoding="UTF-8"?>
<xmlRoot>test for memory leak</xmlRoot>
There is always one Evidence Class that has a strong handle
(using windbg.exe). I know this is cased by the C# code in
the xsl file. If you take the C# out everything works
fine.
I am wonder does anybody know how to solve this issue for
me? My program converts hunderds of this kind file
everyday and it causes big memory leak. I know the
extension obj will work fine but I don't want to go that
rounte at this poing.
Many thanks
Qin Zhou