Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. StreamWriter disposed before call to Close() in destructor

StreamWriter disposed before call to Close() in destructor

Scheduled Pinned Locked Moved C#
questionhelp
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    dene99970
    wrote on last edited by
    #1

    I'm trying to make a logging class and I place SteamWriter.Close() in the destructor, however it seems that Dispose() has already been called on the StreamWriter by the time the destructor is called. Since I cant' call Close(), the text is never flushed to the file. How can I fix this? Turning auto-flush on helps but i still think i should be closing the stream properly rather than letting it happen automatically at program exit.

    E L D 3 Replies Last reply
    0
    • D dene99970

      I'm trying to make a logging class and I place SteamWriter.Close() in the destructor, however it seems that Dispose() has already been called on the StreamWriter by the time the destructor is called. Since I cant' call Close(), the text is never flushed to the file. How can I fix this? Turning auto-flush on helps but i still think i should be closing the stream properly rather than letting it happen automatically at program exit.

      E Offline
      E Offline
      Ennis Ray Lynch Jr
      wrote on last edited by
      #2

      Move your stream reader close call into your dispose method. Call the dispose method from your destructor like:

      private bool mIsDiposed = false;
      ~MyClass(){
      Dispose(false);
      }

      public void Dispose(){
      Dispose(true);
      }

      public void Dispose(bool disposing){
      if(!mIsDisposed){
      //dispose of stuff here
      if(disposing){
      GC.SuppressFinalize(this);
      }
      mIsDisposed = true;
      }
      }

      Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. I also do Android Programming as I find it a refreshing break from the MS. "And they, since they Were not the one dead, turned to their affairs" -- Robert Frost

      1 Reply Last reply
      0
      • D dene99970

        I'm trying to make a logging class and I place SteamWriter.Close() in the destructor, however it seems that Dispose() has already been called on the StreamWriter by the time the destructor is called. Since I cant' call Close(), the text is never flushed to the file. How can I fix this? Turning auto-flush on helps but i still think i should be closing the stream properly rather than letting it happen automatically at program exit.

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        could it be you're already calling StreamWriter.Close or Dispose somewhere? maybe you should show us some of the relevant code. :)

        Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

        Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

        1 Reply Last reply
        0
        • D dene99970

          I'm trying to make a logging class and I place SteamWriter.Close() in the destructor, however it seems that Dispose() has already been called on the StreamWriter by the time the destructor is called. Since I cant' call Close(), the text is never flushed to the file. How can I fix this? Turning auto-flush on helps but i still think i should be closing the stream properly rather than letting it happen automatically at program exit.

          D Offline
          D Offline
          DaveyM69
          wrote on last edited by
          #4

          Are you using a 'using' block by any chance? If so, it automatically calls dispose at the end of the block so manual calls to dispose will cause issues. Another possibility is that some other object (a stream reader or similar) may be closing/disposing the underlying stream. Just a couple of ideas as without code it's very difficult to guess what the problem is.

          Dave

          If this helped, please vote & accept answer!

          Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum.(Pete O'Hanlon)
          BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups