Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3984710
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T05:48:10+00:00 2026-05-20T05:48:10+00:00

I have the following method which creates instances of objects that are disposable. Public

  • 0

I have the following method which creates instances of objects that are disposable.

Public Overridable Sub TransformXmlDocumentsToFileStream(ByVal stream As System.IO.Stream, ByVal xmlDocuments As IEnumerable(Of String), ByVal transformContext As XslTransformContext)
    Dim readers As IEnumerable(Of XmlReader) = _
        (From document In xmlDocuments _
         Select XmlReader.Create(New System.IO.StringReader(document)))

    With transformContext
         TransformXmlDocumentsToFileStream(stream, readers, transformContext)
    End With
End Sub

I then iterate over the objects in another method:

For Each reader In readers
    Using reader
        transform.Transform(reader, writer)
    End Using
Next

The visual studio code analyzer is giving the warning:

CA2000 : Microsoft.Reliability : In method ‘TransformHelper.TransformXmlDocumentsToFileStream(Stream, IEnumerable(Of String), XslTransformContext)’, object ‘New StringReader(document)’ is not disposed along all exception paths. Call System.IDisposable.Dispose on object ‘New StringReader(document)’ before all references to it are out of scope.

Since there is no reference to the StringReader, I cannot put it in a using block or otherwise dispose it. Is it OK to just ignore this warning? The StringReader should be disposed when the reader goes out of scope and is garbage collected, correct?

  • 1 1 Answer
  • 3 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T05:48:11+00:00Added an answer on May 20, 2026 at 5:48 am

    Sorry for the answer in c#. A simple way to manage IDisposables is to make the method that calls new, also call Dispose.

    List<StringReader> stringReaders = new List<StringReader>();
      //note: Select is deferred.  stringReaders is captured.
    IEnumerable<XmlReader> readers =
      xmlDocuments.Select(document =>
      {
        StringReader sr = new System.IO.StringReader(document);
        stringReaders.Add(sr);
        return XmlReader.Create(sr);
      });
    try
    {
      //this method enumerates readers, causing all of the allocations.
      TransformXmlDocumentsToFileStream(stream, readers, transformContext);
    }
    finally
    {
      foreach(StringReader x in stringReaders)
      {
        x.Dispose();
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a library which creates objects (instances of class A) and pass them
I have following method which I am using to load ActiveX control dynamically, Dim
I have the following method which serialises an object to a HTML tag. I
I have the following method which is not capturing anything from the user.If I
I have the following delegate method which is listening for when text changes in
I have a view with which I have a button calling the following method.
I have a CATiledLayer into which I render content in the following method -
I have the following code which is giving me a Method POST, Status (canceled)
I have following method in wcf webenabled service Public Person AddPerson(Person p); As of
I have the following method that is replacing a pound sign from the file

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.