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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T16:45:42+00:00 2026-05-17T16:45:42+00:00

I know this might seem silly, but why does the following code only work

  • 0

I know this might seem silly, but why does the following code only work if I Close() the file? If I don’t close the file, the entire stream is not written.

Steps:

  1. Run this code on form load.
  2. Close form using mouse once it is displayed.
  3. Program terminates.

Shouldn’t the file object be flushed or closed automatically when it goes out of scope? I’m new to C#, but I’m used to adding calls to Close() in C++ destructors.

// Notes: complete output is about 87KB. Without Close(), it's missing about 2KB at the end.

// Convert to png and then convert that into a base64 encoded string.
string b64img = ImageToBase64(img, ImageFormat.Png);
// Save the base64 image to a text file for more testing and external validation.
StreamWriter outfile = new StreamWriter("../../file.txt");
outfile.Write(b64img);
// If we don't close the file, windows will not write it all to disk. No idea why
// that would be.
outfile.Close();
  • 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-17T16:45:43+00:00Added an answer on May 17, 2026 at 4:45 pm

    C# doesn’t have automatic deterministic cleanup. You have to be sure to call the cleanup function if you want to control when it runs. The using block is the most common way of doing this.

    If you don’t put in the cleanup call yourself, then cleanup will happen when the garbage collector decides the memory is needed for something else, which could be a very long time later.

    using (StreamWriter outfile = new StreamWriter("../../file.txt")) {
        outfile.Write(b64img);
    } // everything is ok, the using block calls Dispose which closes the file
    

    EDIT: As Harvey points out, while the cleanup will be attempted when the object gets collected, this isn’t any guarantee of success. To avoid issues with circular references, the runtime makes no attempt to finalize objects in the “right” order, so the FileStream can actually already be dead by the time the StreamWriter finalizer runs and tries to flush buffered output.

    If you deal in objects that need cleanup, do it explicitly, either with using (for locally-scoped usage) or by calling IDisposable.Dispose (for long-lived objects such as referents of class members).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This might seem to be a silly question at first, but please read on.
I know this might seem a controversial question but it really is not meant
I know this might be a no-brainer, but please read on. I also know
I know this question might sound a little cheesy but this is the first
Whether this is possible I don't know, but it would mighty useful! I have
Anyone know this compiler feature? It seems GCC support that. How does it work?
This might seem like a dump question, but my complete ignorance with VbScript is
this might sound like a daft question but I can't seem to find a
I know this isn't strictly a programming question but y'all must have experienced this.
I know this is a broad question, but I've inherited several poor performers and

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.