TextWriter在析构函数中的Close()调用失败 [英] TextWriter fails on Close() call in destructor

查看:110
本文介绍了TextWriter在析构函数中的Close()调用失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个日志类,它使得同步的TextWriter如此,在

构造函数中:

StreamWriter sw = new StreamWriter(filename);

tw = TextWriter.Synchronized(sw);


在析构函数中,~MyLogClass(),我打电话给:


tw.WriteLine(" some stuff");

tw.Close();


Close()调用抛出异常,说我可以不要在

关闭的文件中执行此操作。但是,我不会在任何地方关闭它!在我有机会使用

之前,析构函数会自动关闭文件

吗?资源是'Dispose()自动调用还是什么?什么'

奇怪的是,WriteLine并没有抛出异常(尽管它的

内容从未进入文件)。


Zytan

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

推荐答案

3月22日下午4:58,Zytan < zytanlith ... @ yahoo.comwrote:
On Mar 22, 4:58 pm, "Zytan" <zytanlith...@yahoo.comwrote:

我有一个日志类,它使得同步的TextWriter像这样,在

中构造函数:


StreamWriter sw = new StreamWriter(filename);

tw = TextWriter.Synchronized(sw);


在析构函数中,~MyLogClass(),我调用:


tw.WriteLine(" some stuff");

tw.Close() ;
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();



这不是一个好主意。终结者应该只处理

非托管资源。请记住,当* your * finalizer

运行时,StreamWriter的终结器可能已经运行。这听起来好像你可能根本不想要终结器(它们应该非常罕见) - 基本上只有当你有直接访问非托管的时候才能获得

资源,例如句柄)。


相反,你应该实现IDisposable并告诉来电者打电话

Dispose。

That''s not a good idea to start with. Finalizers should only deal with
unmanaged resources. Bear in mind that by the time *your* finalizer
runs, the StreamWriter''s finalizer may already have run. It sounds
like you may well not want a finalizer at all (they should be very
rare - basically only when you have direct access to unmanaged
resources, eg handles).

Instead, you should implement IDisposable and tell the caller to call
Dispose.


Close()调用抛出一个异常,说我不能在

关闭的文件中执行此操作。但是,我不会在任何地方关闭它!在我有机会使用

之前,析构函数会自动关闭文件

吗?资源是'Dispose()自动调用还是什么?什么是
奇怪的是,WriteLine并没有抛出异常(尽管它的内容从未进入文件)。
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).



基础流可能已经运行了终结器。

(请注意,这与Dispose方法是分开的。通常,使用

MS'的模式,终结器将调用Dispose,但这只是一个实现的问题,而不是CLR为你做的事情。)


Jon

The underlying stream has probably had its finalizer run already.
(Note that this is separate from a Dispose method. Often, and using
MS''s pattern, a finalizer will call Dispose, but that''s just a matter
of implementation, not something the CLR does for you.)

Jon


Jon,作家没有Finalize方法。底层流

有Finalize方法关闭自己。结果是

编写器的缓冲区在流关闭之前从未刷新到流中,导致数据丢失(这是以这种方式设计的) )。我假设

WriteLine执行错误,因为没有尝试刷新封闭流的




Zytan,避免终结者对于非托管代码,因为像这样的东西发生了b $ b。尝试使用如下语法:

using(StreamWriter stream = new FileInfo(@" c:\ test123")。CreateText())

{

stream.WriteLine(" blah blah blah");

}
Jon, writers do not have a Finalize method. The underlying streams
have Finalize methods to close themselves. The result is that the
writer''s buffer is never flushed to the stream before the stream is
closed, resulting in lost data (it''s designed this way). I assume
WriteLine executes error free because no attempt is made to flush to
the closed stream.

Zytan, avoid Finalizers for unmanaged code because stuff like this
happens. Try a syntax like:
using (StreamWriter stream = new FileInfo(@"c:\test123").CreateText())
{
stream.WriteLine("blah blah blah");
}


james< ja ******* *@gmail.com写道:
james <ja********@gmail.comwrote:

Jon,作家没有Finalize方法。
Jon, writers do not have a Finalize method.



是的,但是流可以。我应该在帖子中更精确。

在编写器上调用Close会在流上调用Close,这可能已经完成了b $ b,因此错误。


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet 博客: http://www.msmvps.com/jon.skeet

如果回复该群组,请不要也寄给我

True, but streams do. I should have been more precise in my post.
Calling Close on the writer will call Close on the stream, which may
well have been finalized, hence the error.

--
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


这篇关于TextWriter在析构函数中的Close()调用失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆