473,326 Members | 2,125 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

TextWriter fails on Close() call in destructor

I have a log class that makes a synchronized TextWriter like so, in
the constructor:

StreamWriter sw = new StreamWriter(filename);
tw = TextWriter.Synchronized(sw);

In the destructor, ~MyLogClass(), I call:

tw.WriteLine("some stuff");
tw.Close();

The Close() call throws an exception saying I can't do this on a
closed file. But, I don't close it anywhere! Is the file
automatically closed by the destructor before I get a chance to use
it? Are resources' Dispose() auto-invoked or something? What's
strange is that the WriteLine doesn't throw an exception (although its
contents never make it to the file).

Zytan

Mar 22 '07
54 5101
Zytan <zy**********@gmail.comwrote:
I've ran across this before. I hate using other people's code.
And yet you're using Microsoft's all the time. If you dismiss all third
party libraries, you'll end up reinventing a lot of wheels.

I have no choice with Microsoft, and it is far, far more tested than
such 3rd party code. I don't think that's a fair comparison. In
general, open source software doesn't have great quality, from what
I've seen.
My experience is that the more popular projects have just as good
quality code as many commercial projects.

I've had very good experience with open source projects in general.
I agree reinventing the wheel is bad, in some ways. But, not in terms
of learning. Also, with your own code, you can fix it when things go
bad. Of course, with open source, you can do this, too.
You can both learn and fix with open source code...
I'd rather learn the language myself, and code it myself, this way, I
learn.
Log4net is open source - so download the source and see how they do it.

Good idea. I would be interested in their ideas.
And then you can use it if it works, fix it if it doesn't, and save a
load of time...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 27 '07 #51
Please note that StreamWriter does have a finalizer:
http://msdn2.microsoft.com/en-us/lib...73(VS.71).aspx
StreamWriter.Finalize Method
And this page implies that TextWriter also has a finalizer:
http://msdn2.microsoft.com/en-us/library/ms227564.aspx
TextWriter.Dispose Method ()
Okay dug around the IL for those classes (using ILDasm on
mscorlib.dll). In .Net2.0 there is NO Finalize method for
StreamWriter or TextWriter. In .Net1.1 there is a Finalize method for
StreamWriter only, but this does not flush any data. So microsoft is
wrong Im right :)

-James

Mar 28 '07 #52
On Mar 27, 11:55 pm, "james" <james.w...@gmail.comwrote:
Please note that StreamWriter does have a finalizer:
http://msdn2.microsoft.com/en-us/lib...73(VS.71).aspx
StreamWriter.Finalize Method
And this page implies that TextWriter also has a finalizer:
http://msdn2.microsoft.com/en-us/library/ms227564.aspx
TextWriter.Dispose Method ()

Okay dug around the IL for those classes (using ILDasm on
mscorlib.dll). In .Net2.0 there is NO Finalize method for
StreamWriter or TextWriter. In .Net1.1 there is a Finalize method for
StreamWriter only, but this does not flush any data. So microsoft is
wrong Im right :)
Thanks, James.

Then I wonder what happens when they are already 'finalized' or
whatever by the time I get to them. Does the system ensure they are
flushed if it closes the stream itself? No matter, since I flush
after every write, anyway, just to be sure.

Zytan

Mar 28 '07 #53
On Mar 28, 10:30 am, "Zytan" <zytanlith...@gmail.comwrote:
On Mar 27, 11:55 pm, "james" <james.w...@gmail.comwrote:
Please note that StreamWriter does have a finalizer:
>http://msdn2.microsoft.com/en-us/lib...73(VS.71).aspx
StreamWriter.Finalize Method
And this page implies that TextWriter also has a finalizer:
>http://msdn2.microsoft.com/en-us/library/ms227564.aspx
TextWriter.Dispose Method ()
Okay dug around the IL for those classes (using ILDasm on
mscorlib.dll). In .Net2.0 there is NO Finalize method for
StreamWriter or TextWriter. In .Net1.1 there is a Finalize method for
StreamWriter only, but this does not flush any data. So microsoft is
wrong Im right :)

Thanks, James.

Then I wonder what happens when they are already 'finalized' or
whatever by the time I get to them. Does the system ensure they are
flushed if it closes the stream itself? No matter, since I flush
after every write, anyway, just to be sure.

Zytan
If you don't close the writer it will not get flushed.

Mar 28 '07 #54
If you don't close the writer it will not get flushed.- Hide quoted text -

Ok, thought so, thanks.

Zytan

Mar 28 '07 #55

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: D | last post by:
I have a winforms app that I'm reading some records from a datareader and writing them out to a file like so SqlDataReader dataReader = sqlCommand.ExecuteReader(); TextWriter textWriter = new...
2
by: kengtung | last post by:
Greetings all, I just started programming with C# (.Net FW 1.1) last year and still learning. Previously I am using VB 6.0. Recently, in one application, I encountered a C# error "Cannot write...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.