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 6210325
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:08:23+00:00 2026-05-24T06:08:23+00:00

I joined a project yesterday, we decided to merge our two projects into one,

  • 0

I joined a project yesterday, we decided to merge our two projects into one, and incorporate the features of both (they basically absorbed us).

The problem is, they have a memory leak somewhere, and they tasked me with finding and killing it.

I ran a Code Analysis in Visual Studio and came up with a long list of Warnings, most of which we can ignore, the ones I’m trying to fix are problems with object disposing. Something i’ve never really payed special attention to, and I’m surprised to find out that getting code analysis to decide that something is correct is not easy.

The code Started as this:

StreamWriter SW = new StreamWriter(File.Create("folder/file.txt"));
SW.WriteLine("text");
SW.WriteLine("text");
SW.WriteLine("text");
SW.WriteLine();
SW.WriteLine("text");
SW.WriteLine("text");
SW.WriteLine("text");
SW.Close();
SW.Flush();

But Code Analysis said that File.Create wasn’t disposed of along all code paths and that SW can be disposed of more then once, I went looking and discovered that your not supposed to use both Close and Flush, and in fact, you should use a using statement, so i changed it to this:

using(StreamWriter SW = new StreamWriter(File.Create("folder/file.txt")))
{
    SW.WriteLine("text");
    SW.WriteLine("text");
    SW.WriteLine("text");
    SW.WriteLine();
    SW.WriteLine("text");
    SW.WriteLine("text");
    SW.WriteLine("text");
}

The SW being disposed went away, but it still said that File.Create wasn’t being disposed of along all code paths, there is only one code path, so It didn’t make sense, I tried adding a separate Using statement for File.Create, and Creating/writing the file separately, but code analysis still catches it.

This seems like something that would be easy to figure out, unless VS has a false positive or something.

Anyway, thanks for giving me the time to read this.

~EDIT

I lied in the origional post, when i add the double using statement, including when I use “Stream” it tells me the object can be disposed of more then once, causing an objectdisposedexception. I’m presuming that it would be in some weird freak occurrence. since I never dispose of it myself.

current code:

using (Stream stream = File.Create("folder/file.txt"))
{
    using (StreamWriter SW = new StreamWriter(stream))
    {
        SW.WriteLine("");
        SW.WriteLine("");
        SW.WriteLine("");
        SW.WriteLine();
        SW.WriteLine("");
        SW.WriteLine("");
        SW.WriteLine("");
    }
}

This one worked AND VS Passed it, thank you very much.

using (StreamWriter SW = File.CreateText("text/awardsList.txt"))
{
    SW.WriteLine("");
    SW.WriteLine("");
    SW.WriteLine("");
    SW.WriteLine();
}
  • 1 1 Answer
  • 0 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-24T06:08:24+00:00Added an answer on May 24, 2026 at 6:08 am

    I suspect it’s looking for something like this:

    using (Stream stream = File.Create("folder/file.txt"))
    {
        using (StreamWriter writer = new StreamWriter(stream))
        {
            ...
        }
    }
    

    It’s probably reasonable to assume that if File.Create succeeds, then so will the StreamWriter constructor – but it’s possible that VS doesn’t know that. It may also not know that disposing of the write disposes of the stream.

    Personally I’d use File.CreateText instead though, which avoids the issue and is simpler 🙂

    using (TextWriter writer = File.CreateText("folder/file.txt"))
    {
        ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got two tables that need to be joined via LINQ, but they live
I have recently joined a project that is using multiple different projects. A lot
I am fairly new to Servlet Filters and have basically joined a project using
Just joined a new project which they are using VC#2008 to build an application.
recently I joined two Xcode projects together. To get this thing to work, I
I just joined a new C++ software project and I'm trying to understand the
I just (re-)joined a company to work on some C++ projects. I'm looking at
I have two tables that are joined together. A has many B Normally you
I've got two models, joined by a Has and Belongs To Many join table.
Joined a project a week ago. We use Visual Studio 2008 for C#. How

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.