Home All Groups Group Topic Archive Search About

Is DataSet.GetXml leaking memory?

Author
20 Feb 2007 1:23 PM
Sunil Menon
Dear All,
I was just browsing through my code checking where my application is
allocating memory and found that my code frequently calls
Dataset.GetXml to get xml string output to store data in database.
Further investigation through .Net reflector revealed that the call
creates a stringwriter that it never disposes. Is this a possible
memory leak?
Should the code be using the <using> call when calling a disposable
object?
The following is the code snag-it:
public string GetXml()
{
      string text1;
      IntPtr ptr1;
      Bid.ScopeEnter(out ptr1, "<ds.DataSet.GetXml|API> %d#\n",
this.ObjectID);
      try
      {
            StringWriter writer1 = new
StringWriter(CultureInfo.InvariantCulture);
            if (writer1 != null)
            {
                  XmlTextWriter writer2 = new XmlTextWriter(writer1);
                  writer2.Formatting = Formatting.Indented;
                  new XmlDataTreeWriter(this).Save(writer2, false);
            }
            text1 = writer1.ToString();
      }
      finally
      {
            Bid.ScopeLeave(ref ptr1);
      }
      return text1;
}


Please help.

Thanks & regards
Sunil

Author
20 Feb 2007 4:25 PM
Miha Markic [MVP C#]
StringWriter is very probably not leaking memory. IOW you don't need to
dispose it though it is a good practice to do so.

--
Miha Markic [MVP C#, INETA Country Leader for Slovenia]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/

Show quote
"Sunil Menon" <su***@volcanomail.com> wrote in message
news:1171977831.692570.252500@j27g2000cwj.googlegroups.com...
> Dear All,
> I was just browsing through my code checking where my application is
> allocating memory and found that my code frequently calls
> Dataset.GetXml to get xml string output to store data in database.
> Further investigation through .Net reflector revealed that the call
> creates a stringwriter that it never disposes. Is this a possible
> memory leak?
> Should the code be using the <using> call when calling a disposable
> object?
> The following is the code snag-it:
> public string GetXml()
> {
>      string text1;
>      IntPtr ptr1;
>      Bid.ScopeEnter(out ptr1, "<ds.DataSet.GetXml|API> %d#\n",
> this.ObjectID);
>      try
>      {
>            StringWriter writer1 = new
> StringWriter(CultureInfo.InvariantCulture);
>            if (writer1 != null)
>            {
>                  XmlTextWriter writer2 = new XmlTextWriter(writer1);
>                  writer2.Formatting = Formatting.Indented;
>                  new XmlDataTreeWriter(this).Save(writer2, false);
>            }
>            text1 = writer1.ToString();
>      }
>      finally
>      {
>            Bid.ScopeLeave(ref ptr1);
>      }
>      return text1;
> }
>
>
> Please help.
>
> Thanks & regards
> Sunil
>
Author
2 Mar 2007 2:15 PM
vanko.georgiev
Show quote
On Feb 20, 6:25 pm, "Miha Markic [MVP C#]" <miha at rthand com> wrote:
> StringWriter is very probably not leakingmemory. IOW you don't need to
> dispose it though it is a good practice to do so.
>
> --
> Miha Markic [MVP C#, INETA Country Leader for Slovenia]
> RightHand .NET consulting & developmentwww.rthand.com
> Blog:http://cs.rthand.com/blogs/blog_with_righthand/
>
> "Sunil Menon" <s***@volcanomail.com> wrote in message
>
> news:1171977831.692570.252500@j27g2000cwj.googlegroups.com...
>
> > Dear All,
> > I was just browsing through my code checking where my application is
> > allocatingmemoryand found that my code frequently calls
> >Dataset.GetXmlto get xml string output to store data in database.
> > Further investigation through .Net reflector revealed that the call
> > creates a stringwriter that it never disposes. Is this a possible
> >memoryleak?
> > Should the code be using the <using> call when calling a disposable
> > object?
> > The following is the code snag-it:
> > public string GetXml()
> > {
> >      string text1;
> >      IntPtr ptr1;
> >      Bid.ScopeEnter(outptr1, "<ds.DataSet.GetXml|API> %d#\n",
> > this.ObjectID);
> >      try
> >      {
> >            StringWriter writer1 = new
> > StringWriter(CultureInfo.InvariantCulture);
> >            if (writer1 != null)
> >            {
> >                  XmlTextWriter writer2 = new XmlTextWriter(writer1);
> >                  writer2.Formatting = Formatting.Indented;
> >                  new XmlDataTreeWriter(this).Save(writer2, false);
> >            }
> >            text1 = writer1.ToString();
> >      }
> >      finally
> >      {
> >            Bid.ScopeLeave(ref ptr1);
> >      }
> >      return text1;
> > }
>
> > Please help.
>
> > Thanks & regards
> > Sunil

I have the same problem using getxml() method. Please tell me how can
I solve it...is there any solution?

AddThis Social Bookmark Button