none
CLR Garbage disposal

    Question

  • Hopefully this is sufficiently on topic here.   I am modifying some legacy code, attempting to improve the garbage collection in a CLR function.   Unfortunately, I have very little experience with the objects involved. 

    the code does an xsl transform.  It creates a memory stream when it does the transform, and loads the memory stream into a new SqlXml object and outputs it.   I want to explicitly dispose the memory stream, but when I do, I get the error: 

    'Invalid attempt to call Read when the stream is closed.'

    here is my code in its current state, although I've tried several variations:


     

    public static SqlXml SqlXslTransform(SqlXml XmlData, SqlXml XslData)
    {
    // Load XSL transformation
    System.Xml.Xsl.XslCompiledTransform xform = new System.Xml.Xsl.XslCompiledTransform();
    xform.Load(XslData.CreateReader());
    using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
    {
    xform.Transform(XmlData.CreateReader(), null, ms);
    ms.Seek(0, System.IO.SeekOrigin.Begin);
    // Return the transformed value
    SqlXml retSqlXml = new SqlXml(ms);
    return (retSqlXml);

     

     

    I've tried to figure out how I can declare the retSqlXml early, rather than create it inside the using{} block, but I don't see any sort of load() method.  Is there a way to dispose of the memory stream and still output the SqlXml object, or do I have to leave the stream open?   Is it creating a memory leak if I do?   We are having memory pressure issues on the server caused by this function.

    Any help appreciated.

    Wednesday, November 11, 2009 5:02 PM

Answers

  • As I recall, I ended up re-writing the proc in T-SQL and have avoided using the CLR ever since.   :)


    -Tab Alleman

    • Marked as answer by Tab Alleman 2 Thursday, July 26, 2012 1:51 PM
    Thursday, July 26, 2012 1:50 PM

All replies

  • Hi,

    Perhaps you had any luck with this one? I'm in the same situation. It seems that SqlXml is only a pointer to stream so if the stream is disposed, the SqlXml object is disposed also.

     

    Thanks

    Friday, February 03, 2012 12:22 PM
  • As I recall, I ended up re-writing the proc in T-SQL and have avoided using the CLR ever since.   :)


    -Tab Alleman

    • Marked as answer by Tab Alleman 2 Thursday, July 26, 2012 1:51 PM
    Thursday, July 26, 2012 1:50 PM