StreamWriter disposed before call to Close() in destructor
-
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.
-
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.
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
-
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.
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.
-
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.
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)